| |
| |
| |
| |
| |
| |
| |
|
|
| const chalk = require('chalk'); |
| const fs = require('fs'); |
| const nodePlop = require('node-plop'); |
| const path = require('path'); |
| const rimraf = require('rimraf'); |
| const shell = require('shelljs'); |
|
|
| const addCheckmark = require('./helpers/checkmark'); |
| const xmark = require('./helpers/xmark'); |
|
|
| |
| |
| |
| |
| const { BACKUPFILE_EXTENSION } = require('../generators/index'); |
|
|
| process.chdir(path.join(__dirname, '../generators')); |
|
|
| const plop = nodePlop('./index.js'); |
| const componentGen = plop.getGenerator('component'); |
| const containerGen = plop.getGenerator('container'); |
| const languageGen = plop.getGenerator('language'); |
|
|
| |
| |
| |
| |
| const NAMESPACE = 'RbGenerated'; |
|
|
| |
| |
| |
| |
| |
| function prettyStringify(data) { |
| return JSON.stringify(data, null, 2); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function handleResult({ changes, failures }) { |
| return new Promise((resolve, reject) => { |
| if (Array.isArray(failures) && failures.length > 0) { |
| reject(new Error(prettyStringify(failures))); |
| } |
|
|
| resolve(changes); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| function feedbackToUser(info) { |
| return result => { |
| console.info(chalk.blue(info)); |
| return result; |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| function reportSuccess(message) { |
| return result => { |
| addCheckmark(() => console.log(chalk.green(` ${message}`))); |
| return result; |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| function reportErrors(reason) { |
| |
| xmark(() => console.error(chalk.red(` ${reason}`))); |
| process.exit(1); |
| } |
|
|
| |
| |
| |
| |
| |
| function runLintingOnDirectory(relativePath) { |
| return new Promise((resolve, reject) => { |
| shell.exec( |
| `npm run lint:eslint "app/${relativePath}/**/**.js"`, |
| { |
| silent: true, |
| }, |
| code => |
| code |
| ? reject(new Error(`Linting error(s) in ${relativePath}`)) |
| : resolve(relativePath), |
| ); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| function runLintingOnFile(filePath) { |
| return new Promise((resolve, reject) => { |
| shell.exec( |
| `npm run lint:eslint "${filePath}"`, |
| { |
| silent: true, |
| }, |
| code => { |
| if (code) { |
| reject(new Error(`Linting errors in ${filePath}`)); |
| } else { |
| resolve(filePath); |
| } |
| }, |
| ); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| function removeDir(relativePath) { |
| return new Promise((resolve, reject) => { |
| try { |
| rimraf(path.join(__dirname, '/../../app/', relativePath), err => { |
| if (err) throw err; |
| }); |
| resolve(relativePath); |
| } catch (err) { |
| reject(err); |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| function removeFile(filePath) { |
| return new Promise((resolve, reject) => { |
| try { |
| fs.unlink(filePath, err => { |
| if (err) throw err; |
| }); |
| resolve(filePath); |
| } catch (err) { |
| reject(err); |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async function restoreModifiedFile( |
| filePath, |
| backupFileExtension = BACKUPFILE_EXTENSION, |
| ) { |
| return new Promise((resolve, reject) => { |
| const targetFile = filePath.replace(`.${backupFileExtension}`, ''); |
| try { |
| fs.copyFile(filePath, targetFile, err => { |
| if (err) throw err; |
| }); |
| resolve(targetFile); |
| } catch (err) { |
| reject(err); |
| } |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async function generateComponent({ name, memo }) { |
| const targetFolder = 'components'; |
| const componentName = `${NAMESPACE}Component${name}`; |
| const relativePath = `${targetFolder}/${componentName}`; |
| const component = `component/${memo ? 'Pure' : 'NotPure'}`; |
|
|
| await componentGen |
| .runActions({ |
| name: componentName, |
| memo, |
| wantMessages: true, |
| wantLoadable: true, |
| }) |
| .then(handleResult) |
| .then(feedbackToUser(`Generated '${component}'`)) |
| .catch(reason => reportErrors(reason)); |
| await runLintingOnDirectory(relativePath) |
| .then(reportSuccess(`Linting test passed for '${component}'`)) |
| .catch(reason => reportErrors(reason)); |
| await removeDir(relativePath) |
| .then(feedbackToUser(`Cleanup '${component}'`)) |
| .catch(reason => reportErrors(reason)); |
|
|
| return component; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| async function generateContainer({ name, memo }) { |
| const targetFolder = 'containers'; |
| const componentName = `${NAMESPACE}Container${name}`; |
| const relativePath = `${targetFolder}/${componentName}`; |
| const container = `container/${memo ? 'Pure' : 'NotPure'}`; |
|
|
| await containerGen |
| .runActions({ |
| name: componentName, |
| memo, |
| wantHeaders: true, |
| wantActionsAndReducer: true, |
| wantSagas: true, |
| wantMessages: true, |
| wantLoadable: true, |
| }) |
| .then(handleResult) |
| .then(feedbackToUser(`Generated '${container}'`)) |
| .catch(reason => reportErrors(reason)); |
| await runLintingOnDirectory(relativePath) |
| .then(reportSuccess(`Linting test passed for '${container}'`)) |
| .catch(reason => reportErrors(reason)); |
| await removeDir(relativePath) |
| .then(feedbackToUser(`Cleanup '${container}'`)) |
| .catch(reason => reportErrors(reason)); |
|
|
| return container; |
| } |
|
|
| |
| |
| |
| |
| |
| async function generateComponents(components) { |
| const promises = components.map(async component => { |
| let result; |
|
|
| if (component.kind === 'component') { |
| result = await generateComponent(component); |
| } else if (component.kind === 'container') { |
| result = await generateContainer(component); |
| } |
|
|
| return result; |
| }); |
|
|
| const results = await Promise.all(promises); |
|
|
| return results; |
| } |
|
|
| |
| |
| |
| |
| |
| async function generateLanguage(language) { |
| |
| const generatedFiles = await languageGen |
| .runActions({ language, test: true }) |
| .then(handleResult) |
| .then(feedbackToUser(`Added new language: '${language}'`)) |
| .then(changes => |
| changes.reduce((acc, change) => { |
| const pathWithRemovedAnsiEscapeCodes = change.path.replace( |
| |
| /(\u001b\[3(?:4|9)m)/g, |
| '', |
| ); |
| const obj = {}; |
| obj[pathWithRemovedAnsiEscapeCodes] = change.type; |
| return Object.assign(acc, obj); |
| }, {}), |
| ) |
| .catch(reason => reportErrors(reason)); |
|
|
| |
| const lintingTasks = Object.keys(generatedFiles) |
| .filter( |
| filePath => |
| generatedFiles[filePath] === 'modify' || |
| generatedFiles[filePath] === 'add', |
| ) |
| .filter(filePath => filePath.endsWith('.js')) |
| .map(async filePath => { |
| const result = await runLintingOnFile(filePath) |
| .then(reportSuccess(`Linting test passed for '${filePath}'`)) |
| .catch(reason => reportErrors(reason)); |
|
|
| return result; |
| }); |
|
|
| await Promise.all(lintingTasks); |
|
|
| |
| const restoreTasks = Object.keys(generatedFiles) |
| .filter(filePath => generatedFiles[filePath] === 'backup') |
| .map(async filePath => { |
| const result = await restoreModifiedFile(filePath) |
| .then( |
| feedbackToUser( |
| `Restored file: '${filePath.replace( |
| `.${BACKUPFILE_EXTENSION}`, |
| '', |
| )}'`, |
| ), |
| ) |
| .catch(reason => reportErrors(reason)); |
|
|
| return result; |
| }); |
|
|
| await Promise.all(restoreTasks); |
|
|
| |
| const removalTasks = Object.keys(generatedFiles) |
| .filter( |
| filePath => |
| generatedFiles[filePath] === 'backup' || |
| generatedFiles[filePath] === 'add', |
| ) |
| .map(async filePath => { |
| const result = await removeFile(filePath) |
| .then(feedbackToUser(`Removed '${filePath}'`)) |
| .catch(reason => reportErrors(reason)); |
|
|
| return result; |
| }); |
|
|
| await Promise.all(removalTasks); |
|
|
| return language; |
| } |
|
|
| |
| |
| |
| (async function () { |
| await generateComponents([ |
| { kind: 'component', name: 'Component', memo: false }, |
| { kind: 'component', name: 'MemoizedComponent', memo: true }, |
| { kind: 'container', name: 'Container', memo: false }, |
| { kind: 'container', name: 'MemoizedContainer', memo: true }, |
| ]).catch(reason => reportErrors(reason)); |
|
|
| await generateLanguage('fr').catch(reason => reportErrors(reason)); |
| })(); |
|
|