UNPKG

ng-afelio

Version:
288 lines (251 loc) 11.6 kB
// const util = require('util'); // const exec = require('child_process').exec; const spawn = require('child_process').spawn; const colors = require('colors'); const fs = require('fs'); const fse = require('fs-extra'); const { join, basename } = require('path'); const { watchTree } = require('watch'); // const cli = require('@angular/cli'); // const addLocalCli = require('./scripts/add-local-cli'); // const uiKitTypes = require('./models/ui-kit-types.enum'); const config = require('./config'); const { version } = require('./package.json'); const { getConfig } = require('./scripts/check-files/util'); const currentPath = process.cwd(); // function promiseFromChildProcess(child) { // return new Promise(function (resolve, reject) { // child.stdout.pipe(process.stdout); // child.stderr.pipe(process.stderr); // child.addListener("error", reject); // child.addListener("exit", resolve); // }); // } // pexec = (command, options) => promiseFromChildProcess(exec(command, options)); pexec = (command, options) => new Promise(function (resolve, reject) { const process = spawn(command, { stdio: 'inherit', shell: true, ...options }); process.on('close', function (code) { resolve(code); }); process.on('error', function (err) { console.error(err); reject(err); }); }); // const getAngularVersion = async () => { // return await cli.default({ cliArgs: ['--version'] }); // } const createNewProject = async (name, uiKitType, isOpenApi, ngOptionsString, angularVersion) => { if (isOpenApi) { console.info(`Creating project ${name}`); // await cli.default({ cliArgs: ['new', name, '--create-application=false', '--new-project-root=apis', '--skip-install', ...produceNgOptions(ngOptionsString)] }); await pexec(`npx @angular/cli@${angularVersion} new ${name} --create-application=false --new-project-root=apis ${ngOptionsString || ''}`); console.info(`${colors.green('Project created')}`); process.chdir(`./${name}`); console.info(`Adding ng-afelio`); const ngAfelioSrc = config.production ? `ng-afelio@${version}` : __dirname; // await cli.default({ cliArgs: ['add', ngAfelioSrc, '--skip-confirmation', '--ui-kit=none'] }); await pexec(`npx ng add ${ngAfelioSrc} --skip-confirmation --ui-kit=none`); console.info(`${colors.green('ng-afelio installed')}`); console.info(`Creating library project`); // await cli.default({ cliArgs: ['generate', 'library', 'api', '--prefix=lib'] }); await pexec(`npx ng generate library api --prefix=lib`); console.info(`${colors.green('Library created')}`); console.info(`Install dependencies`); await pexec('npm install ng-openapi-gen@latest gulp@4.0.2 gulp-replace@1.0.0 gulp-rename@2.0.0 minimist@1.2.8 --save-dev'); console.info(`${colors.green('Dependencies installed')}`); console.info(`Apply template`); if (fs.rm) { fs.rmSync('apis/api/src/lib', { recursive: true, force: true }); fs.rmSync('apis/api/src/public-api.ts', { force: true }); } else { fs.rmdirSync('apis/api/src/lib', { recursive: true }); fs.unlinkSync('apis/api/src/public-api.ts'); } const subPackageJson = 'apis/api/package.json.tmpl'; fs.renameSync('apis/api/package.json', subPackageJson); const packageJsonFileContent = JSON.parse(fs.readFileSync(subPackageJson, { encoding: 'utf8' })); packageJsonFileContent.name = `\${scopeName}\${apiName}`; packageJsonFileContent.version = '${apiVersion}'; packageJsonFileContent.publishConfig = { 'registry': '${apiVersion}' }; fs.writeFileSync(subPackageJson, JSON.stringify(packageJsonFileContent, null, 2), { encoding: 'utf8' }); const packageJsonPath = './package.json'; const packageJsonContent = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' })); packageJsonContent.scripts = { 'ng': 'ng', 'build': 'ng build', 'ng-afelio': 'ng-afelio', 'ng-swagger-gen': 'ng-openapi-gen', 'regenerate-api': 'ng-afelio api -r ng-swagger-gen-api.json', 'install-deps': 'export NG_CLI_ANALYTICS=ci && npm ci && ng analytics off', 'prepare-workspace': 'gulp', 'package': 'npm run regenerate-api && npm run build', 'publish': 'npm publish dist/api' } fs.writeFileSync(packageJsonPath, JSON.stringify(packageJsonContent, null, 2), { encoding: 'utf8' }); const currentPath = process.cwd(); const templatePath = join(__dirname, 'templates/generate-swagger'); await fse.copy(templatePath, currentPath); console.info(`${colors.green('Template applied')}`); } else { // await cli.default({ cliArgs: ['new', name, '--routing', '--style=scss', '--skip-install', ...produceNgOptions(ngOptionsString)] }); await pexec(`npx @angular/cli@${angularVersion} new ${name} --routing --standalone=false --style=scss ${ngOptionsString || ''}`); process.chdir(`./${name}`); const ngAfelioSrc = config.production ? `ng-afelio@${version}` : __dirname; await pexec(`npx ng add ${ngAfelioSrc} --skip-confirmation --ui-kit=${uiKitType}`); await pexec(`npx ng generate @schematics/angular:environments`); // await cli.default({ cliArgs: ['add', ngAfelioSrc, '--skip-confirmation', `--ui-kit=${uiKitType}`] }); } // process.chdir(`./${name}`); // if (uiKitType !== uiKitTypes.NONE) { // const { fillUiKit, runUiKit } = require('./scripts/generate-ui-kit'); // await cli.default({cliArgs: ['generate', 'application', 'ui-kit', '--routing', '--style=scss', '--skip-tests']}); // await fillUiKit(uiKitType); // await runUiKit(); // } // return await addLocalCli(uiKitType !== uiKitTypes.NONE); // await cli.default({cliArgs: ['new', name, '--routing', '--style=scss', `--collection=${__dirname}`, ...produceNgOptions(ngOptionsString)]}); }; const serveUIKit = async (port, ngOptionsString) => { // return await cli.default({cliArgs: [ // 'serve', // 'ui-kit', // `--port=${port||'5200'}`, // '--host=0.0.0.0', // ...produceNgOptions(ngOptionsString) // ]}); return await pexec(`npx ng serve ui-kit --port=${port || '5200'} --host=0.0.0.0 ${produceNgOptions(ngOptionsString).join(' ')}`, { cwd: currentPath }).then(code => { if (code > 0) { console.log(colors.yellow('Do you have a ui-kit project ?')); } return code; }); } const serveMain = async (environment, port, ngOptionsString, projectName) => { // return await cli.default({cliArgs: [ // 'serve', // `--port=${port||'4200'}`, // '--host=0.0.0.0', // ...( environment ? [`--configuration=${environment}`] : []), // ...produceNgOptions(ngOptionsString) // ]}); if (!projectName) { projectName = getConfig('defaultProject'); } return await pexec(`npx ng serve ${projectName || ''} --port=${port || '4200'} --host=0.0.0.0 ${environment ? [`--configuration=${environment}`] : ''} ${produceNgOptions(ngOptionsString).join(' ')}`, { cwd: currentPath }); } const generate = async (type, name, ngOptions) => { type = `ng-afelio:${type}` // return await cli.default({ cliArgs: ['generate', type, ...(name ? [name] : []), ...ngOptions] }); return await pexec(`npx ng generate ${type} ${name || ''} ${ngOptions.join(' ')}`); } const generateApi = (source, moduleName, apiKey, extract, version, proxy) => { const { generateSwagger } = require('./scripts/generate-swagger'); return generateSwagger(source, moduleName, apiKey, extract, version, proxy); } const regenerateApi = (source) => { const { regenerateSwagger } = require('./scripts/generate-swagger'); return regenerateSwagger(source); }; const build = async (environment, ssr, baseHref, ngOptionsString) => { // const baseArgs = [ // 'build', // '--prod', // `--configuration=${environment}`, // ...( baseHref ? [`--base-href=${baseHref}`] : []), // ...produceNgOptions(ngOptionsString) // ]; if (ssr) { console.warn(colors.underline(colors.yellow('Not implemented yet.'))); } else { return await pexec(`npx ng build --configuration=${environment} ${baseHref ? `--base-href=${baseHref}` : ''} ${produceNgOptions(ngOptionsString).join('')}`, { cwd: currentPath }); // return await cli.default({cliArgs: baseArgs}); } } const buildStyle = async (watchPath) => { try { const { buildStyleFromUIKit } = require('./scripts/build-style'); if (watchPath) { await new Promise(function () { let first = true; let locked = false; watchTree( join(currentPath, watchPath), { ignoreDotFiles: true, ignoreNotPermitted: true, ignoreUnreadableDir: true, filter(path) { const fileName = basename(path); return !fileName.includes('.') || fileName.endsWith('.scss') }, ignoreDirectoryPattern: /node_modules/ }, async function() { if (!locked) { locked = true; try { await buildStyleFromUIKit(first); first = false; } catch(e){ console.error(e); } locked = false; } } ); }); } else { await buildStyleFromUIKit(); } } catch (e) { console.error(e); console.warn(colors.red('You must be in base folder of the application and have a "ui-kit" project to use this command.')); } } // const generateMocks = async () => { // if (currentPath.endsWith('/src')) { // return await generateMocksTask(); // } else { // console.warn(colors.red('You must be in src folder to generate mocks.')); // } // } const checkFiles = async (type, mainFile) => { try { if (type === 'environment') { const checkEnvFiles = require('./scripts/check-files/check-env-files'); return await checkEnvFiles(mainFile); } else { const checkI18nFiles = require('./scripts/check-files/check-i18n-files').checkFiles; return await checkI18nFiles(mainFile); } } catch (e) { // console.error(colors.red('Unexpected error', e)); } } const generateI18n = async (mainFile) => { const { fixI18n } = require('./scripts/check-files/check-i18n-files'); return fixI18n(mainFile); } function produceNgOptions(ngOptionsString) { let ngOptions = []; if (ngOptionsString) { ngOptions = ngOptionsString.split(' '); } return ngOptions; } // Export all methods module.exports = { // getAngularVersion, createNewProject, serveUIKit, serveMain, generate, build, buildStyle, // generateMocks, generateApi, regenerateApi, checkFiles, generateI18n };