diff --git a/plopfile.js b/plopfile.js index 5c3790a..d43d0bd 100644 --- a/plopfile.js +++ b/plopfile.js @@ -1,102 +1,5 @@ -const fs = require('fs'); -const rushJSON = require('./rush.json'); +const createPackageGenerator = require('./tools/plop/create-package-generator'); module.exports = function (plop) { - plop.setGenerator('create-package', { - description: 'Creates a new component package', - prompts: [ - { - type: 'list', - name: 'package-type', - message: 'What type of package?', - choices: [...rushJSON.approvedPackagesPolicy.reviewCategories], - }, - { - type: 'input', - name: 'package-name', - message: 'What is the package name?', - }, - { - type: 'input', - name: 'package-description', - message: 'What is the component package description?', - }, - ], - actions: function (data) { - data['package-raw-name'] = data['package-name']; - - const packagePaths = data['package-name'].split('.'); - let rootPath = '{{package-type}}/{{kebabCase package-name}}'; - - if (packagePaths.length > 1) { - data['package-raw-name'] = packagePaths.pop(); - rootPath = `{{package-type}}/${packagePaths.join( - '/' - )}/{{kebabCase package-raw-name}}`; - } - - return [ - { - type: 'add', - path: `${rootPath}/src/index.ts`, - templateFile: 'tools/plop-templates/create-package/package-index.hbs', - skipIfExists: true, - }, - { - type: 'add', - path: `${rootPath}/src/{{pascalCase package-raw-name}}.tsx`, - templateFile: 'tools/plop-templates/create-package/package-name.hbs', - skipIfExists: true, - }, - { - type: 'add', - path: `${rootPath}/package.json`, - templateFile: 'tools/plop-templates/create-package/package.hbs', - skipIfExists: true, - }, - { - type: 'add', - path: `${rootPath}/tsconfig.json`, - templateFile: 'tools/plop-templates/create-package/tsconfig.hbs', - skipIfExists: true, - }, - { - type: 'add', - path: `${rootPath}/.eslintrc`, - templateFile: 'tools/plop-templates/create-package/eslintrc.hbs', - skipIfExists: true, - }, - { - type: 'add', - path: `${rootPath}/.eslintignore`, - templateFile: 'tools/plop-templates/create-package/eslintignore.hbs', - skipIfExists: true, - }, - function registerPackageInRushJSON(data) { - const packageName = `@bereghici/${data['package-name']}`; - - if (rushJSON.projects.map(p => p.packageName).includes(packageName)) { - console.log('[SKIPPED] Package already registered in rush.json'); - return; - } - - rushJSON.projects.push({ - packageName, - projectFolder: [data['package-type'], data['package-name']] - .join('/') - .replace('.', '/'), - reviewCategory: data['package-type'], - }); - - fs.writeFile('./rush.json', JSON.stringify(rushJSON), err => { - if (err) { - console.log('Error saving rush.json', err); - } else { - console.log('Successfully added package in rush.json'); - } - }); - }, - ]; - }, - }); + plop.setGenerator('create-package', createPackageGenerator(plop)); }; diff --git a/tools/plop-templates/create-package/package.hbs b/tools/plop-templates/create-package/package.hbs deleted file mode 100644 index c4ae9b9..0000000 --- a/tools/plop-templates/create-package/package.hbs +++ /dev/null @@ -1,26 +0,0 @@ -{ - "name": "@bereghici/{{package-name}}", - "version": "0.0.1", - "description": "{{sentenceCase package-description}}", - "scripts": { - "build": "bereghici-scripts build --bundle cjs,esm", - "lint":"bereghici-scripts lint 'src/**/*.{ts,tsx}'", - "pre-commit": "bereghici-scriptspre-commit", - "typecheck": "bereghici-scripts typecheck", - "validate":"bereghici-scripts validate" - }, - "main": "dist/{{package-name}}.cjs.js", - "module": "dist/{{package-name}}.esm.js", - "types": "dist/index.d.ts", - "dependencies": { - "@babel/runtime": "^7.14.0" - }, - "devDependencies": { - "@bereghici/bereghici-scripts": "0.0.1", - "@bereghici/eslint-config": "0.0.1", - "typescript": "^4.3.2" - }, - "peerDependencies": { - "typescript": "^4.3.2" - } -} \ No newline at end of file diff --git a/tools/plop/create-package-generator.js b/tools/plop/create-package-generator.js new file mode 100644 index 0000000..b0fa5f5 --- /dev/null +++ b/tools/plop/create-package-generator.js @@ -0,0 +1,102 @@ +const fs = require('fs'); +const rushJSON = require('../../rush.json'); + +module.exports = function () { + return { + description: 'Creates a new component package', + prompts: [ + { + type: 'list', + name: 'package-type', + message: 'What type of package?', + choices: [...rushJSON.approvedPackagesPolicy.reviewCategories], + }, + { + type: 'input', + name: 'package-name', + message: 'What is the package name?', + }, + { + type: 'input', + name: 'package-description', + message: 'What is the component package description?', + }, + ], + actions: function (data) { + data['package-raw-name'] = data['package-name']; + + const packagePaths = data['package-name'].split('.'); + let rootPath = '{{package-type}}/{{kebabCase package-name}}'; + + if (packagePaths.length > 1) { + data['package-raw-name'] = packagePaths.pop(); + rootPath = `{{package-type}}/${packagePaths.join( + '/' + )}/{{kebabCase package-raw-name}}`; + } + + return [ + { + type: 'add', + path: `${rootPath}/src/index.ts`, + templateFile: 'tools/plop-templates/create-package/package-index.hbs', + skipIfExists: true, + }, + { + type: 'add', + path: `${rootPath}/src/{{pascalCase package-raw-name}}.tsx`, + templateFile: 'tools/plop-templates/create-package/package-name.hbs', + skipIfExists: true, + }, + { + type: 'add', + path: `${rootPath}/package.json`, + templateFile: 'tools/plop-templates/create-package/package.hbs', + skipIfExists: true, + }, + { + type: 'add', + path: `${rootPath}/tsconfig.json`, + templateFile: 'tools/plop-templates/create-package/tsconfig.hbs', + skipIfExists: true, + }, + { + type: 'add', + path: `${rootPath}/.eslintrc`, + templateFile: 'tools/plop-templates/create-package/eslintrc.hbs', + skipIfExists: true, + }, + { + type: 'add', + path: `${rootPath}/.eslintignore`, + templateFile: 'tools/plop-templates/create-package/eslintignore.hbs', + skipIfExists: true, + }, + function registerPackageInRushJSON(data) { + const packageName = `@bereghici/${data['package-name']}`; + + if (rushJSON.projects.map(p => p.packageName).includes(packageName)) { + console.log('[SKIPPED] Package already registered in rush.json'); + return; + } + + rushJSON.projects.push({ + packageName, + projectFolder: [data['package-type'], data['package-name']] + .join('/') + .replace('.', '/'), + reviewCategory: data['package-type'], + }); + + fs.writeFile('./rush.json', JSON.stringify(rushJSON), err => { + if (err) { + console.log('Error saving rush.json', err); + } else { + console.log('Successfully added package in rush.json'); + } + }); + }, + ]; + }, + }; +}; diff --git a/tools/plop-templates/create-package/eslintignore.hbs b/tools/plop/templates/create-package/eslintignore.hbs similarity index 100% rename from tools/plop-templates/create-package/eslintignore.hbs rename to tools/plop/templates/create-package/eslintignore.hbs diff --git a/tools/plop-templates/create-package/eslintrc.hbs b/tools/plop/templates/create-package/eslintrc.hbs similarity index 100% rename from tools/plop-templates/create-package/eslintrc.hbs rename to tools/plop/templates/create-package/eslintrc.hbs diff --git a/tools/plop-templates/create-package/package-index.hbs b/tools/plop/templates/create-package/package-index.hbs similarity index 100% rename from tools/plop-templates/create-package/package-index.hbs rename to tools/plop/templates/create-package/package-index.hbs diff --git a/tools/plop-templates/create-package/package-name.hbs b/tools/plop/templates/create-package/package-name.hbs similarity index 100% rename from tools/plop-templates/create-package/package-name.hbs rename to tools/plop/templates/create-package/package-name.hbs diff --git a/tools/plop/templates/create-package/package.hbs b/tools/plop/templates/create-package/package.hbs new file mode 100644 index 0000000..e109ba4 --- /dev/null +++ b/tools/plop/templates/create-package/package.hbs @@ -0,0 +1,10 @@ +{ "name": "@bereghici/{{package-name}}", "version": "0.0.1", "description": "{{sentenceCase + package-description +}}", "scripts": { "build": "bereghici-scripts build --bundle cjs,esm", +"lint":"bereghici-scripts lint 'src/**/*.{ts,tsx}'", "pre-commit": +"bereghici-scriptspre-commit", "typecheck": "bereghici-scripts typecheck", +"validate":"bereghici-scripts validate" }, "main": "dist/{{package-name}}.cjs.js", +"module": "dist/{{package-name}}.esm.js", "types": "dist/index.d.ts", +"dependencies": { "@babel/runtime": "^7.14.0" }, "devDependencies": { +"@bereghici/bereghici-scripts": "0.0.1", "@bereghici/eslint-config": "0.0.1", +"typescript": "^4.3.2" }, "peerDependencies": { "typescript": "^4.3.2" } } \ No newline at end of file diff --git a/tools/plop-templates/create-package/tsconfig.hbs b/tools/plop/templates/create-package/tsconfig.hbs similarity index 100% rename from tools/plop-templates/create-package/tsconfig.hbs rename to tools/plop/templates/create-package/tsconfig.hbs