<commit_before>'use strict';

const childProcess = require('child_process');
const erector = require('erector-set');
const fs = require('fs');
const path = require('path');
const utilities = require('../utilities');

module.exports = (rootDir) => {
    erector.inquire(getQuestions(), true, getPreviousTransforms()).then((answers) => {
        const srcDir = path.resolve(rootDir, 'src');
        let templateList = [
            { destination: path.resolve(rootDir, '.gitignore'), name: '__gitignore' },
            { destination: path.resolve(rootDir, '.npmignore'), name: '__npmignore' },
            { name: 'DEVELOPMENT.md' },
            { blank: true, name: 'examples/example.component.html' },
            { blank: true, name: 'examples/example.component.scss' },
            { name: 'examples/example.component.ts' },
            { name: 'examples/example.main.ts' },
            { name: 'examples/example.module.ts' },
            { name: 'examples/index.html' },
            { blank: true, name: 'examples/styles.scss' },
            { name: 'index.ts' },
            { name: 'karma.conf.js', overwrite: true },
            { destination: path.resolve(srcDir, '{{ name }}.module.ts'), name: 'src/module.ts' },
            { name: 'package.json', update: 'json' },
            { name: 'README.md' },
            { destination: path.resolve(srcDir, 'index.ts'), name: 'src/index.ts' },
            { destination: path.resolve(srcDir, 'test.js'), name: 'src/test.js', overwrite: true },
            { name: 'tsconfig.json', overwrite: true },
            { name: 'tsconfig.es2015.json', overwrite: true },
            { name: 'tsconfig.es5.json', overwrite: true },
            { name: 'tsconfig.test.json', overwrite: true },
            { name: 'tslint.json', overwrite: true },
            { destination: path.resolve(srcDir, 'vendor.ts'), name: 'src/vendor.ts' },
            { name: 'tasks/build.js', overwrite: true },
            { name: 'tasks/copy-build.js', overwrite: true },
            { name: 'tasks/copy-globs.js', overwrite: true },
            { name: 'tasks/inline-resources.js', overwrite: true },
            { name: 'tasks/rollup.js', overwrite: true },
            { name: 'tasks/test.js', overwrite: true },
            { name: 'webpack/webpack.common.js', overwrite: true },
            { name: 'webpack/webpack.dev.js', overwrite: true },
            { name: 'webpack/webpack.test.js', overwrite: true },
            { name: 'webpack/webpack.utils.js', overwrite: true }
        ];
        const gitAnswer = answers.find((answer) => answer.name === 'git');
        const startingDir = __dirname;
        const templates = utilities.getTemplates(rootDir, __dirname, templateList);

        erector.construct(answers, templates);

        process.chdir(rootDir);
        if (gitAnswer.answer) {
            initGit(rootDir);
        }

        console.info('Installing Node modules');
        execute('npm i');
        console.info('Node modules installed');
        process.chdir(startingDir);
    });
};

const getQuestions = () => {
    const defaultName = require(path.resolve(process.cwd(), 'package.json')).name;

    return [
        { defaultAnswer: defaultName, name: 'name', question: `Library name:`, transform: checkNameFormat },
        { defaultAnswer: (answers) => utilities.dashToWords(answers[0].answer), name: 'readmeTitle', question: 'README Title:' },
        { name: 'repoUrl', question: 'Repository URL:' },
        { name: 'git', question: 'Reinitialize Git project (y/N)?', transform: utilities.createYesNoValue('n') },
        { name: 'moduleName', useAnswer: 'name', transform: (value) => utilities.dashToCap(value) + 'Module' },
        { name: 'version', question: 'Version:' }
    ];
};

const checkNameFormat = (value) => {
    if (!value) {
        value = '';
    } else if (!utilities.checkIsDashFormat(value)) {
        value = null;
    }

    return value;
}

const getPreviousTransforms = () => ({
    git: utilities.convertYesNoValue
});
<commit_msg>#59 Initial scoped package support<commit_after>'use strict';

const childProcess = require('child_process');
const erector = require('erector-set');
const fs = require('fs');
const path = require('path');
const utilities = require('../utilities');

module.exports = (rootDir) => {
    erector.inquire(getQuestions(), true, getPreviousTransforms()).then((answers) => {
        const srcDir = path.resolve(rootDir, 'src');
        let templateList = [
            { destination: path.resolve(rootDir, '.gitignore'), name: '__gitignore' },
            { destination: path.resolve(rootDir, '.npmignore'), name: '__npmignore' },
            { name: 'DEVELOPMENT.md' },
            { blank: true, name: 'examples/example.component.html' },
            { blank: true, name: 'examples/example.component.scss' },
            { name: 'examples/example.component.ts' },
            { name: 'examples/example.main.ts' },
            { name: 'examples/example.module.ts' },
            { name: 'examples/index.html' },
            { blank: true, name: 'examples/styles.scss' },
            { name: 'index.ts' },
            { name: 'karma.conf.js', overwrite: true },
            { destination: path.resolve(srcDir, '{{ name }}.module.ts'), name: 'src/module.ts' },
            { name: 'package.json', update: 'json' },
            { name: 'README.md' },
            { destination: path.resolve(srcDir, 'index.ts'), name: 'src/index.ts' },
            { destination: path.resolve(srcDir, 'test.js'), name: 'src/test.js', overwrite: true },
            { name: 'tsconfig.json', overwrite: true },
            { name: 'tsconfig.es2015.json', overwrite: true },
            { name: 'tsconfig.es5.json', overwrite: true },
            { name: 'tsconfig.test.json', overwrite: true },
            { name: 'tslint.json', overwrite: true },
            { destination: path.resolve(srcDir, 'vendor.ts'), name: 'src/vendor.ts' },
            { name: 'tasks/build.js', overwrite: true },
            { name: 'tasks/copy-build.js', overwrite: true },
            { name: 'tasks/copy-globs.js', overwrite: true },
            { name: 'tasks/inline-resources.js', overwrite: true },
            { name: 'tasks/rollup.js', overwrite: true },
            { name: 'tasks/test.js', overwrite: true },
            { name: 'webpack/webpack.common.js', overwrite: true },
            { name: 'webpack/webpack.dev.js', overwrite: true },
            { name: 'webpack/webpack.test.js', overwrite: true },
            { name: 'webpack/webpack.utils.js', overwrite: true }
        ];
        const gitAnswer = answers.find((answer) => answer.name === 'git');
        const startingDir = __dirname;
        const templates = utilities.getTemplates(rootDir, __dirname, templateList);

        erector.construct(answers, templates);

        process.chdir(rootDir);
        if (gitAnswer.answer) {
            initGit(rootDir);
        }

        console.info('Installing Node modules');
        execute('npm i');
        console.info('Node modules installed');
        process.chdir(startingDir);
    });
};

const getQuestions = () => {
    const defaultName = require(path.resolve(process.cwd(), 'package.json')).name;

    return [
        { defaultAnswer: defaultName, name: 'name', question: `Library name:`, transform: checkNameFormat },
        { defaultAnswer: (answers) => utilities.dashToWords(answers[0].answer), name: 'readmeTitle', question: 'README Title:' },
        { name: 'repoUrl', question: 'Repository URL:' },
        { name: 'git', question: 'Reinitialize Git project (y/N)?', transform: utilities.createYesNoValue('n') },
        { name: 'moduleName', useAnswer: 'name', transform: (value) => generateModuleName(value) },
        { name: 'version', question: 'Version:' }
    ];
};

const checkNameFormat = (name) => {
    if (!name) {
        name = '';
    } else if (!checkPackageName(name)) {
        name = null;
    }

    return name;
};

const checkPackageName = (name) =>
    (checkIsScopedName(name) && !checkScopedName(name)) ||
        utilities.checkIsDashFormat(name);

const checkIsScopedName = (name) =>
    typeof name === 'string' &&
    name[0] === '@' &&
    name.split('/').length === 2;

const checkScopedName = (name) => {
    const parts = name.split('/');

    return checkScopeFormat(parts[0]) && utilities.checkIsDashFormat(parts[1]);
};

const checkScopeFormat = (scope) => /^@[a-z][a-zA-Z0-9]*$/.test(scope);

const generateModuleName = (value) => {
    if (checkIsScopedName(value)) {
        moduleName = utilities.dashToCap(value.split('/')[1]);
    } else {
        moduleName = utilities.dashToCap(value);
    }

    return `${ moduleName }Module`;
};

const getPreviousTransforms = () => ({
    git: utilities.convertYesNoValue
});

{'fraction_non_alphanumeric': 0.17042863657090743, 'fraction_numerical': 0.0023939808481532147, 'mean_word_length': 2.646300914380715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, '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': '3795819', 'n_tokens_mistral': 2669, 'n_tokens_neox': 2554, 'n_words': 544}