Skip to content

Commit

Permalink
Add bundler and ability to opt out
Browse files Browse the repository at this point in the history
  • Loading branch information
ianpogi5 committed May 14, 2020
1 parent af20c65 commit a160886
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 16 deletions.
4 changes: 3 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ program
.usage(`${chalk.green('<package-name>')}`)
.arguments('<package-name>')
.option('--info', 'print environment debug info')
.option('--no-bundle', 'do not bundle the build output')
.action((name) => {
packageName = name;
})
.parse(process.argv);

const createPackage = async () => {
try {
await run({ packageName });
const { bundle } = program;
await run({ packageName, bundle });
log(chalk.green('\n\nYour package is ready!\n\n'));
log(chalk.blue(`\tcd ${packageName}`));
log(chalk.blue('\tcode .\n\n'));
Expand Down
57 changes: 48 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { log } = console;

const PKG_DIR = __dirname;

const run = async ({ packageName }) => {
const run = async ({ packageName, bundle }) => {
try {
log(chalk.green.bold('Creating your package...'));
log(chalk`Package Name: {blue ${packageName}}`);
Expand All @@ -30,7 +30,9 @@ const run = async ({ packageName }) => {
await cmd('npm', ['init', '-y']);

log(chalk.green('Installing dev dependencies...'));
await cmd('npm', ['i', '-D', ...NPM_PACKAGES_DEV]);
const devDeps = [...NPM_PACKAGES_DEV];
if (bundle) devDeps.push('parcel@next');
await cmd('npm', ['i', '-D', ...devDeps]);

log(chalk.green('Installing prod dependencies...'));
await cmd('npm', ['i', ...NPM_PACKAGES_PROD]);
Expand All @@ -55,6 +57,11 @@ const run = async ({ packageName }) => {
);

log(chalk.green('Updating package.json...'));
const mods = [];
mods.push({ field: 'version', value: '0.1.0' });
mods.push({ field: 'main', value: 'lib/index.js' });
mods.push({ field: 'license', value: 'MIT' });

const scripts = {
'build:commonjs': 'babel src --out-dir lib',
clean: 'rm -fR lib',
Expand All @@ -65,20 +72,52 @@ const run = async ({ packageName }) => {
coverage: 'jest --coverage',
};

pkg.mod([
{ field: 'version', value: '0.1.0' },
{ field: 'main', value: 'lib/index.js' },
{ field: 'license', value: 'MIT' },
{ field: 'scripts', value: scripts },
]);
if (bundle) {
scripts.bundle = 'parcel build src/index.js';
scripts.build = 'npm run clean && npm run bundle';
mods.push({
field: 'targets',
value: {
main: {
engines: {
node: '>=10.x',
},
},
},
});
}

mods.push({ field: 'scripts', value: scripts });

const { stdout: userName } = await cmd(
'git',
['config', 'user.name'],
false
);
const { stdout: email } = await cmd('git', ['config', 'user.email'], false);
pkg.mod([{ field: 'author', value: { name: userName, email } }]);
mods.push({ field: 'author', value: { name: userName, email } });

mods.push({
field: 'husky',
value: {
hooks: {
'pre-commit': 'lint-staged',
},
},
});

mods.push({
field: 'lint-staged',
value: {
'*.js': [
'eslint src --ext .js --fix',
'pretty-quick --staged',
'git add',
],
},
});

pkg.mod(mods);

log(chalk.green('Initializing git...'));
await cmd('git', ['init']);
Expand Down
74 changes: 68 additions & 6 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ const ROOT_DIR = process.cwd();
const PKG_DIR = `${ROOT_DIR}/${PKG_NAME}`;

describe('Create NodeJs', () => {
beforeAll(async () => {
beforeEach(async () => {
await fse.remove(PKG_DIR);
});

afterAll(async () => {
afterEach(async () => {
await fse.remove(PKG_DIR);
});

it('should run', async () => {
jest.setTimeout(60000);
it('should run with bundler', async () => {
jest.setTimeout(120000);
expect.assertions(
17 +
22 +
Object.keys(CONFIG_FILES).length +
NPM_PACKAGES_DEV.length +
NPM_PACKAGES_PROD.length
);

try {
await run({ packageName: 'hello-world' });
await run({ packageName: 'hello-world', bundle: true });
expect(fs.existsSync(PKG_DIR)).toBe(true);
expect(fs.existsSync(`${PKG_DIR}/package.json`)).toBe(true);
expect(fs.existsSync(`${PKG_DIR}/.git`)).toBe(true);
Expand All @@ -53,8 +53,70 @@ describe('Create NodeJs', () => {
expect(pkg.scripts).toHaveProperty('test');
expect(pkg.scripts).toHaveProperty('watch');
expect(pkg.scripts).toHaveProperty('coverage');
expect(pkg.scripts).toHaveProperty('bundle');
expect(pkg.scripts.build).toMatch(/.*bundle$/);
expect(pkg.author).toHaveProperty('name');
expect(pkg.author).toHaveProperty('email');
expect(pkg).toHaveProperty('husky');
expect(pkg).toHaveProperty('lint-staged');
expect(pkg).toHaveProperty('targets');

NPM_PACKAGES_DEV.forEach((c) => {
expect(pkg.devDependencies).toHaveProperty(c);
});

NPM_PACKAGES_PROD.forEach((c) => {
expect(pkg.dependencies).toHaveProperty(c);
});
} catch (error) {
// console.log(error);
expect(error).toBe(null);
}
});

it('should run without bundler', async () => {
jest.setTimeout(120000);
expect.assertions(
22 +
Object.keys(CONFIG_FILES).length +
NPM_PACKAGES_DEV.length +
NPM_PACKAGES_PROD.length
);

try {
await run({ packageName: 'hello-world', bundle: false });
expect(fs.existsSync(PKG_DIR)).toBe(true);
expect(fs.existsSync(`${PKG_DIR}/package.json`)).toBe(true);
expect(fs.existsSync(`${PKG_DIR}/.git`)).toBe(true);

Object.keys(CONFIG_FILES).forEach((k) => {
const c = CONFIG_FILES[k];
expect(fs.existsSync(`${PKG_DIR}/${c}`)).toBe(true);
});

// template source files
expect(fs.existsSync(`${PKG_DIR}/src/index.js`)).toBe(true);
expect(fs.existsSync(`${PKG_DIR}/tests/index.test.js`)).toBe(true);

const contents = fs.readFileSync(`${PKG_DIR}/package.json`, 'utf-8');
const pkg = JSON.parse(contents);
expect(pkg.version).toBe('0.1.0');
expect(pkg.main).toBe('lib/index.js');
expect(pkg.license).toBe('MIT');
expect(pkg.scripts).toHaveProperty('build:commonjs');
expect(pkg.scripts).toHaveProperty('clean');
expect(pkg.scripts).toHaveProperty('build');
expect(pkg.scripts.build).toMatch(/.*commonjs$/);
expect(pkg.scripts).toHaveProperty('lint');
expect(pkg.scripts).toHaveProperty('test');
expect(pkg.scripts).toHaveProperty('watch');
expect(pkg.scripts).toHaveProperty('coverage');
expect(pkg.scripts).not.toHaveProperty('bundle');
expect(pkg.author).toHaveProperty('name');
expect(pkg.author).toHaveProperty('email');
expect(pkg).toHaveProperty('husky');
expect(pkg).toHaveProperty('lint-staged');
expect(pkg).not.toHaveProperty('targets');

NPM_PACKAGES_DEV.forEach((c) => {
expect(pkg.devDependencies).toHaveProperty(c);
Expand Down

0 comments on commit a160886

Please sign in to comment.