Skip to content

Commit

Permalink
chore: extracted create-package generator in a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandru Bereghici committed Sep 10, 2021
1 parent d1ee316 commit a942c3f
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 125 deletions.
101 changes: 2 additions & 99 deletions plopfile.js
Original file line number Diff line number Diff line change
@@ -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));
};
26 changes: 0 additions & 26 deletions tools/plop-templates/create-package/package.hbs

This file was deleted.

102 changes: 102 additions & 0 deletions tools/plop/create-package-generator.js
Original file line number Diff line number Diff line change
@@ -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');
}
});
},
];
},
};
};
10 changes: 10 additions & 0 deletions tools/plop/templates/create-package/package.hbs
Original file line number Diff line number Diff line change
@@ -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" } }

0 comments on commit a942c3f

Please sign in to comment.