
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
common-boilerplate
Advanced tools
base class for boilerplate
use create-common-boilerplate for quick start.
$ npm init common-boilerplate
- ask question
- list all file from boilerplate paths
- render files to target dir
- do post jobs
βββ bin
β βββ cli.js
β
βββ boilerplate
β βββ lib
β βββ test
β βββ README.md
β βββ _.eslintrc
β βββ _.gitignore
β βββ _package.json
β βββ index.js
β
βββ test
β βββ index.test.js
βββ index.js
βββ README.md
βββ package.json
index.js is your Boilerplate Logic, the main entry.boilerplate/** is your template dir, will be copy to dest.// index.js
const Boilerplate = require('common-boilerplate');
class MainBoilerplate extends Boilerplate {
// must provide your directory
get [Symbol.for('boilerplate#root')]() {
return __dirname;
}
};
module.exports = MainBoilerplate;
Inquirer is built-in to provide prompt helper.
Add your questions:
class MainBoilerplate extends Boilerplate {
async askQuestions() {
const answers = await this.prompt([
{
name: 'name',
type: 'input',
message: 'Project Name: ',
default: () => this.locals.name, // set default from locals
},
{
type: 'list',
name: 'type',
message: 'choose your type:',
choices: [ 'simple', 'plugin', 'framework' ],
},
]);
this.setLocals(answers);
// use built-in questions
await this.askGit();
}
};
Built-in Questions:
askNpm(): ask for name / scope / description, and pkgName getter.askGit(): ask for repositorythis.locals is used to fill the template, it's merge from built-in -> argv -> user's prompt answer;
Built-in:
name - project name, by default to git repository nameuser - user info
name - git config user.nameemail - git config user.emailauthor - ${user} <${email}>gitInfo - git url info
git config remote.origin.urlnpm - npm global cli name, will guest by order: tnpm -> cnpm -> npmregistry - npm registry url, not set by defaultBuilt-in render is nunjucks.
And use micromatch to match this.templateRules to treat as template.
this.templateRules = [ '!res/**' ];
{{name}}.test.js is supported.boilerplate/package.json, npm will read files and ignore your files._ as prefix, such as _package.json / _.gitignore / _.eslintrcthis.fileMappingDefault mappings:
this.fileMapping = {
gitignore: '.gitignore',
_gitignore: '.gitignore',
'_.gitignore': '.gitignore',
'_package.json': 'package.json',
'_.eslintrc': '.eslintrc',
'_.eslintignore': '.eslintignore',
'_.npmignore': '.npmignore',
};
Provide powerful cli logger for developer, see consola for more details.
debug is disabled by default, use --verbose or DEBUG= to print all logs.
this.logger.info('this is info log');
this.logger.level = 'DEBUG';
Provide httpclient for developer, see urllib for more details.
await this.request(url, opts);
Use this.requestOpts as default request options.
Provide runscript for developer, see runscript for more details.
cwd is set to target dir, and will use this.local.npm as cli.
await this.runScript('ci', { grep: 'home.test.js' }, {});
await this.installDeps({ optional : false });
await this.runTest({});
Also support custom argv:
argv will convert to camelCase, such as --page-size=1 -> pageSize--page.size=1 -> { page: { size: '1' } }class MainBoilerplate extends Boilerplate {
// use as `--test=123 --str=456`
initOptions() {
const options = Object.assign({}, super.initOptions());
options.test = {
type: 'string',
description: 'just a test',
};
options.str = {
type: 'string',
description: 'just a str',
};
return options;
}
};
Built-in:
--baseDir= - directory of application, default to process.cwd()--npm= - npm cli, tnpm/cnpm/npm, will auto guess--registry= - npm registry url, also support alias -r=china, will auto guest from npm cli.--force - force to override directory if it's not emptySupport mutli-level boilerplate, so you can share logic between boilerplates.
class ShareBoilerplate extends Boilerplate {
// must provide your directory
get [Symbol.for('boilerplate#root')]() {
return __dirname;
}
};
module.exports = ShareBoilerplate;
// child
class MainBoilerplate extends ShareBoilerplate {
// must provide your directory
get [Symbol.for('boilerplate#root')]() {
return __dirname;
}
// example for ignore some files from parent
async listFiles(...args) {
const files = await super.listFiles(...args);
files['github.png'] = undefined;
return files;
}
};
module.exports = MainBoilerplate;
Symbol.for('boilerplate#root') to announce your root, and boilerplate directory is required to exists at your root directory.async listFiles(), such as ignore some files from parent.Use Coffee and assert-file.
const coffee = require('coffee');
const assertFile = require('assert-file');
const { rimraf, mkdirp } = require('mz-modules');
describe('test/index.test.js', () => {
const fixtures = path.join(__dirname, 'fixtures');
const tmpDir = path.join(__dirname, '.tmp');
beforeEach(async () => {
await rimraf(tmpDir);
await mkdirp(tmpDir);
});
it('should work', async () => {
// run cli
await coffee.fork(path.join(fixtures, 'simple/bin/cli.js'), [], { cwd: tmpDir })
// .debug()
// tell coffee to listen prompt event then auto answer
.waitForPrompt()
// answer to the questions
.writeKey('example\n')
.writeKey('ENTER')
// emit `DOWN` key to select the second choise
.writeKey('DOWN', 'ENTER')
.expect('stdout', /npm install --no-package-lock/)
.expect('stdout', /1 passing/)
.expect('code', 0)
.end();
// expect to be exists
assertFile(`${tmpDir}/.gitignore`);
// check with `includes`
assertFile(`${tmpDir}/README.md`, 'name = example');
// check with regex
assertFile(`${tmpDir}/README.md`, /name = example/);
// check whether contains json
assertFile(`${tmpDir}/package.json`, {
name: 'example',
boilerplate: {
name: 'common-boilerplate-test-project',
version: '1.0.0',
}
});
});
});
FAQs
base class for boilerplate
The npm package common-boilerplate receives a total of 0 weekly downloads. As such, common-boilerplate popularity was classified as not popular.
We found that common-boilerplate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Β It has 13 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.