Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
feat: templates
Browse files Browse the repository at this point in the history
  • Loading branch information
arantespp committed Aug 16, 2020
1 parent 52ee751 commit 3bfb81c
Show file tree
Hide file tree
Showing 39 changed files with 615 additions and 208 deletions.
4 changes: 4 additions & 0 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const getConfig = () => {
};

yargs
.scriptName('pepe')
.env('PEPE')
.pkgConf('pepe')
.config(getConfig())
Expand All @@ -54,6 +55,9 @@ yargs
hidden: true,
},
})
/**
* Create final options with environment and environments.
*/
.middleware((argv) => {
const { environment, environments } = argv;
if (environment) {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/deploy/stackName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getStackName = async () => {
}

if (environment) {
return `${pascalCase(packageName)}-${pascalCase(environment)}`;
return `${pascalCase(packageName)}-${environment}`;
}

if (currentBranch) {
Expand Down
31 changes: 11 additions & 20 deletions packages/cli/src/deploy/staticApp/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,27 @@ import { destroyCloudFormation } from '../cloudFormation';

import { deployStaticApp } from './staticApp';

export const deployStaticAppCommand: CommandModule<
{ destroy: boolean },
{
buildFolder: string;
cloudfront: boolean;
destroy?: boolean;
edge: boolean;
hostedZoneName?: string;
}
> = {
export const deployStaticAppCommand: CommandModule = {
command: 'static-app',
describe: 'Static app deploy.',
builder: (yargs) =>
yargs
.options({
acmArn: {
aliases: ['acm-arn'],
'acm-arn': {
conflicts: 'acmArnExportedName',
group: 'aaa',
type: 'string',
},
acmArnExportedName: {
aliases: ['acm-arn-exported-name'],
'acm-arn-exported-name': {
conflicts: 'acmArn',
group: 'aaa',
type: 'string',
},
aliases: {
describe: 'CloudFront aliases.',
type: 'array',
},
buildFolder: {
aliases: ['build-folder'],
'build-folder': {
default: 'build',
type: 'string',
},
Expand All @@ -49,17 +39,18 @@ export const deployStaticAppCommand: CommandModule<
require: false,
type: 'boolean',
},
hostedZoneName: {
aliases: ['hosted-zone-name'],
'hosted-zone-name': {
required: false,
type: 'string',
},
})
.middleware((argv) => {
const { acmArn, acmArnExportedName, aliases, edge } = argv;
const { acmArn, aliases, edge } = argv;
if (acmArn || acmArn || aliases || edge) {
argv.cloudfront = true;
}
})
.check(({ aliases, acmArnExportedName, acmArn }) => {
if (aliases && !(acmArn || acmArnExportedName)) {
throw new Error(
'"alias" is defined but "acm-arn" or "acm-arn-exported-name" is not.',
Expand All @@ -70,7 +61,7 @@ export const deployStaticAppCommand: CommandModule<
if (destroy) {
destroyCloudFormation();
} else {
deployStaticApp(rest);
deployStaticApp(rest as any);
}
},
};
34 changes: 31 additions & 3 deletions packages/cli/src/monorepo/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@ import { CommandModule } from 'yargs';

import { monorepo } from './monorepo';

export const monorepoCommand: CommandModule = {
command: 'monorepo',
export const monorepoCommand: CommandModule<
any,
{ name?: string; update?: boolean; skipInstall?: boolean }
> = {
command: 'monorepo [name]',
describe: 'Update the repository to monorepo.',
builder: (yargs) => yargs,
builder: (yargs) => {
yargs.positional('name', {
describe: 'Monorepo name.',
conflicts: 'update',
type: 'string',
});
yargs.options({
skipInstall: {
aliases: ['skip-install'],
required: false,
type: 'boolean',
},
update: {
conflicts: 'name',
required: false,
type: 'boolean',
},
});
yargs.check(({ name, update }) => {
if (name || update) {
return true;
}
throw new Error('"name" or "update" must exists.');
});
return yargs;
},
handler: monorepo,
};
61 changes: 61 additions & 0 deletions packages/cli/src/monorepo/getConfigs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Exported to be added to documentation.
*/

import { templates } from './templates';

export const monorepoTemplates = templates.map(({ content, ...rest }) => ({
...rest,
content: content.trim(),
}));

export const packagesWillBeInstalled = [
'@commitlint/cli',
'@commitlint/config-conventional',
'@typescript-eslint/eslint-plugin',
'@typescript-eslint/parser',
'eslint',
'eslint-config-airbnb',
'eslint-config-airbnb-base',
'eslint-config-prettier',
'eslint-config-react-app',
'eslint-plugin-flowtype',
'eslint-plugin-import',
'eslint-plugin-jest',
'eslint-plugin-jest-dom',
'eslint-plugin-jsx-a11y',
'eslint-plugin-prettier',
'eslint-plugin-react',
'eslint-plugin-react-hooks',
'eslint-plugin-relay',
'husky',
'imagemin-lint-staged',
'jest',
'jest-emotion',
'lerna',
'lint-staged',
'npm-check-updates',
'pepe-cli',
'prettier',
'stylelint',
'stylelint-config-prettier',
'stylelint-prettier',
'ts-jest',
'ts-node',
'typescript',
];

export const packageJsonProperties = {
name: '<name>',
private: true,
scripts: {
'list-packages': 'lerna ls',
'list-updates': 'npx ncu && lerna exec -- npx ncu',
reinstall: 'yarn run remove-all-node-modules && yarn',
'remove-all-node-modules':
'npx lerna exec -- rm -rf node_modules && rm -rf node_modules && rm -f yarn.lock',
'update-all': 'npx ncu -u && lerna exec -- npx ncu -u',
version: 'lerna version',
},
workspaces: ['packages/**/*'],
};
148 changes: 49 additions & 99 deletions packages/cli/src/monorepo/monorepo.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,23 @@
import { paramCase } from 'change-case';
import deepMerge from 'deepmerge';
import fs from 'fs';
import log from 'npmlog';
import path from 'path';

import { exec, writeTemplate, TemplateParams } from '../utils';
import { exec, writeFileTemplates } from '../utils';

const logPrefix = 'monorepo';

const writeFiles = async () => {
const getFilesArr = (defaultTemplateParams: TemplateParams = {}) => {
const files: Array<{
templateName: string;
writeDir: string;
templateParams?: Partial<TemplateParams>;
}> = [
{ templateName: 'fileMock.js', writeDir: '__mocks__' },
{ templateName: 'styleMock.js', writeDir: '__mocks__' },
{ templateName: 'dot-eslintignore', writeDir: '.' },
{ templateName: 'dot-eslintrc.js', writeDir: '.' },
{ templateName: 'dot-gitignore', writeDir: '.' },
{ templateName: 'dot-huskyrc.js', writeDir: '.' },
{ templateName: 'dot-prettierrc.js', writeDir: '.' },
{ templateName: 'dot-stylelintrc.js', writeDir: '.' },
{ templateName: 'commitlint.config.js', writeDir: 'config' },
{ templateName: 'eslintrc.base.js', writeDir: 'config' },
{ templateName: 'eslintrc.react.js', writeDir: 'config' },
{ templateName: 'husky.config.js', writeDir: 'config' },
{ templateName: 'jest.config.base.js', writeDir: 'config' },
{ templateName: 'lerna.json', writeDir: '.' },
{ templateName: 'lint-staged.config.js', writeDir: 'config' },
{ templateName: 'prettier.config.js', writeDir: 'config' },
{ templateName: 'stylelint.config.js', writeDir: 'config' },
{ templateName: 'tsconfig.base.json', writeDir: 'config' },
{ templateName: 'tsconfig.react.json', writeDir: 'config' },
];
import {
monorepoTemplates,
packageJsonProperties,
packagesWillBeInstalled,
} from './getConfigs';

return files.map(({ templateName, writeDir, templateParams }) => ({
templateName,
writeDir,
templateDir: path.resolve(__dirname, 'templates'),
templateParams: { ...defaultTemplateParams, ...templateParams },
}));
};
const logPrefix = 'monorepo';

const writeMonorepoTemplates = async () => {
try {
log.info(logPrefix, 'Writing files...');
await Promise.all(getFilesArr().map((file) => writeTemplate(file)));
/**
* Create "packages" folder.
*/
await fs.promises.mkdir('packages');
await writeFileTemplates(monorepoTemplates);
} catch (error) {
log.error(logPrefix, 'Cannot write files.');
log.error(logPrefix, 'Error message: %j', error.message);
Expand All @@ -58,77 +27,58 @@ const writeFiles = async () => {
const installDependencies = async () => {
try {
log.info(logPrefix, 'Installing dependencies...');
await exec(
[
'yarn add -DW',
'@commitlint/cli',
'@commitlint/config-conventional',
'@typescript-eslint/eslint-plugin',
'@typescript-eslint/parser',
'eslint',
'eslint-config-airbnb',
'eslint-config-airbnb-base',
'eslint-config-prettier',
'eslint-config-react-app',
'eslint-plugin-flowtype',
'eslint-plugin-import',
'eslint-plugin-jest',
'eslint-plugin-jest-dom',
'eslint-plugin-jsx-a11y',
'eslint-plugin-prettier',
'eslint-plugin-react',
'eslint-plugin-react-hooks',
'eslint-plugin-relay',
'husky',
'imagemin-lint-staged',
'jest',
'jest-emotion',
'lerna',
'lint-staged',
'npm-check-updates',
'pepe-cli',
'prettier',
'stylelint',
'stylelint-config-prettier',
'stylelint-prettier',
'ts-jest',
'ts-node',
'typescript',
].join(' '),
);
await exec(['yarn add -DW', ...packagesWillBeInstalled].join(' '));
} catch (error) {
log.error(logPrefix, 'Cannot install dependencies.');
log.error(logPrefix, 'Error message: %j', error.message);
}
};

const updatePackageJson = () => {
const updatePackageJson = ({ name }: { name?: string }) => {
try {
log.info(logPrefix, 'Updating package.json...');
const packageJsonPath = path.resolve(process.cwd(), 'package.json');
// eslint-disable-next-line @typescript-eslint/no-var-requires, global-require, import/no-dynamic-require
const packageJson = require(packageJsonPath);
packageJson.private = true;
packageJson.scripts = {
...packageJson.scripts,
'list-packages': 'lerna ls',
'list-updates': 'npx ncu && lerna exec -- npx ncu',
'update-all': 'npx ncu -u && lerna exec -- npx ncu -u',
reinstall: 'yarn run remove-all-node-modules && yarn',
'remove-all-node-modules':
'npx lerna exec -- rm -rf node_modules && rm -rf node_modules && rm -f yarn.lock',
const packageJson = fs.existsSync(packageJsonPath)
? // eslint-disable-next-line @typescript-eslint/no-var-requires, global-require, import/no-dynamic-require
require(packageJsonPath)
: {};
const newPackageJsonProperties = {
...packageJsonProperties,
name: name ? paramCase(name) : packageJson.name || '',
};
packageJson.workspaces = ['packages/**/*'];
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
const newPackageJson = deepMerge(packageJson, newPackageJsonProperties);
newPackageJson.workspaces = [...new Set(newPackageJson.workspaces)];
fs.writeFileSync(packageJsonPath, JSON.stringify(newPackageJson, null, 2));
} catch (error) {
log.error(logPrefix, 'Cannot update package.json.');
log.error(logPrefix, 'Error message: %j', error.message);
}
};

export const monorepo = async () => {
log.info(logPrefix, 'Updating monorepo...');
await writeFiles();
await installDependencies();
updatePackageJson();
export const monorepo = async ({
name,
skipInstall,
update,
}: {
name?: string;
skipInstall?: boolean;
update?: boolean;
}) => {
if (update) {
log.info(logPrefix, 'Updating monorepo...');
} else {
if (!name) {
throw new Error('"name" is not defined.');
}
log.info(logPrefix, 'Creating monorepo...');
await fs.promises.mkdir(name);
process.chdir(path.resolve(process.cwd(), name));
await fs.promises.mkdir('packages');
await exec('npm init -y');
}
await writeMonorepoTemplates();
if (!skipInstall) {
await installDependencies();
}
updatePackageJson({ name });
};
6 changes: 5 additions & 1 deletion packages/cli/src/monorepo/templates/commitlint.config.js.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export default `
export const templateName = 'commitlint.config.js';

export const dir = '.';

export const content = `
const fs = require('fs');
/**
Expand Down
Loading

0 comments on commit 3bfb81c

Please sign in to comment.