<commit_before>import {YangGenerator} from "../yang.generator";
import * as path from "path";
import * as extend from "deep-extend";
import chalk from "chalk";
import {EOL} from 'os';
import {Argv} from "yargs";
import {YangFeatureGenerator} from "../feature/yang-feature.generator";
import {YangHomeGenerator} from "../home/yang-home.generator";
import {FileUtils} from "../../helpers/file-utils";

const commandExists = require('command-exists').sync;

export class YangAppGenerator extends YangGenerator
{
    _initializing(): void {
        super._initializing();
        this.props['name'] = this.options['name'] || path.basename(process.cwd());
        this.props['description'] = this.props.name;
    }


    _configuring(): void {
        super._configuring();
        this.props['dir'] = this.options['dir'] || `./${this.props['name']}`;

        // Surcharge de la racine pour ce répertoire
        this.root = this.directory;

        // Si le répertoire n'est pas vide : STOP
        if (FileUtils.exists(this.destinationPath(`${this.projectRoot}package.json`))) {
            console.log(chalk.bgRed(`Directory [${this.projectRoot}] is not empty !`));
            process.exit(1);
        }
    }


    async _writing(): Promise<void> {
        await super._writing();

        // Launch angular-cli
        if (!commandExists('ng')) {
            console.log(chalk.bgRed(`Angular CLI not available. Install it with 'npm i -g @angular/cli' or 'yarn global add @angular/cli'.`));
            process.exit(1);
        }

        this.spawnCommandSync('ng', ['new', this.props.name
            , '--style', 'scss'
            , '--directory', this.root
            , '--inline-style'
            , '--inline-template'
            // , '--skip-git'
            , '--skip-commit'
            , '--skip-install'
        ]);

        await this.copyTemplates();

        this.updatePolyfills();
        this.updatePackageJson();
        this.updateGitIgnore();

        await this.composeWith(new YangFeatureGenerator(), {
            'name': 'home',
            'with-component': true,
            'with-template': true,
            'with-styles': true
        });

        // Recopier les templates du home
        await this.composeWith(new YangHomeGenerator());

        // Faire le git commit
        if (commandExists('git')) {
            let spawnOpts = { cwd: this.root, stdio: 'ignore' };
            this.spawnCommandSync('git', ['add', '.'], spawnOpts);
            this.spawnCommandSync('git', ['commit', '-m', 'chore: initial commit from yang-cli'], spawnOpts);
        }
    }


    async _install() {
        await super._install();

        if (this.options['install'])
            this.spawnCommandSync("yarn", [], { cwd: this.props['dir']});
    }



    updatePolyfills(): void {
        const file = path.join(this.root, 'src', 'polyfills.ts');
        let content = FileUtils.read(file);
        content += `import 'whatwg-fetch';${EOL}`;

        FileUtils.write(file, content);
    }<commit_msg>feat: Add skip-git flag to generate a new app<commit_after>import {YangGenerator} from "../yang.generator";
import * as path from "path";
import * as extend from "deep-extend";
import chalk from "chalk";
import {EOL} from 'os';
import {Argv} from "yargs";
import {YangFeatureGenerator} from "../feature/yang-feature.generator";
import {YangHomeGenerator} from "../home/yang-home.generator";
import {FileUtils} from "../../helpers/file-utils";

const commandExists = require('command-exists').sync;

export class YangAppGenerator extends YangGenerator
{
    private skipGit: boolean = false;

    static yargs(yargs: Argv): Argv {
        return super.yargs(yargs)
            .option('skip-git', { type: 'boolean', default: false, describe: 'Skip git' })
        ;
    }

    _initializing(): void {
        super._initializing();
        this.props['name'] = this.options['name'] || path.basename(process.cwd());
        this.props['description'] = this.props.name;
        this.skipGit = this.options['skip-git'] || false;
    }


    _configuring(): void {
        super._configuring();
        this.props['dir'] = this.options['dir'] || `./${this.props['name']}`;

        // Surcharge de la racine pour ce répertoire
        this.root = this.directory;

        // Si le répertoire n'est pas vide : STOP
        if (FileUtils.exists(this.destinationPath(`${this.projectRoot}package.json`))) {
            console.log(chalk.bgRed(`Directory [${this.projectRoot}] is not empty !`));
            process.exit(1);
        }
    }


    async _writing(): Promise<void> {
        await super._writing();

        // Launch angular-cli
        if (!commandExists('ng')) {
            console.log(chalk.bgRed(`Angular CLI not available. Install it with 'npm i -g @angular/cli' or 'yarn global add @angular/cli'.`));
            process.exit(1);
        }

        let ngOpts = ['new', this.props.name
            , '--style', 'scss'
            , '--directory', this.root
            , '--inline-style'
            , '--inline-template'
            , '--skip-install'
        ];

        if (this.skipGit)
            ngOpts.push('--skip-git');
        else
            ngOpts.push('--skip-commit');


        this.spawnCommandSync('ng', ngOpts);

        await this.copyTemplates();

        this.updatePolyfills();
        this.updatePackageJson();

        await this.composeWith(new YangFeatureGenerator(), {
            'name': 'home',
            'with-component': true,
            'with-template': true,
            'with-styles': true
        });

        // Recopier les templates du home
        await this.composeWith(new YangHomeGenerator());

        // Faire le git commit
        if (!this.skipGit) {
            this.updateGitIgnore();

            if (commandExists('git')) {
                let spawnOpts = { cwd: this.root, stdio: 'ignore' };
                this.spawnCommandSync('git', ['add', '.'], spawnOpts);
                this.spawnCommandSync('git', ['commit', '-m', 'chore: initial commit from yang-cli'], spawnOpts);
            }
        }
    }


    async _install() {
        await super._install();

        if (this.options['install'])
            this.spawnCommandSync("yarn", [], { cwd: this.props['dir']});
    }



    updatePolyfills(): void {
        const file = path.join(this.root, 'src', 'polyfills.ts');
        let content = FileUtils.read(file);
        content += `import 'whatwg-fetch';${EOL}`;

        FileUtils.write(file, content);
    }
{'fraction_non_alphanumeric': 0.15917114351496547, 'fraction_numerical': 0.0006139677666922486, 'mean_word_length': 2.233746898263027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '238563', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1885, 'n_words': 425}