Skip to content

Commit

Permalink
fix(config): ncm config updates config files from template repo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenyulin committed Dec 5, 2018
1 parent 9f55fe2 commit a44b3f3
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 92 deletions.
18 changes: 18 additions & 0 deletions src/actions/common-steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import cosmiconfig from 'cosmiconfig';
import { exec } from 'lib/child-process';

// TODO: setup schema and sanitise config file
export const readConfig = async () => {
const MODULE_NAME = 'ncm';
const { config } = await cosmiconfig(MODULE_NAME).search();
return config;
};

export const cloneTemplateRepo = async config => {
const DEFAULT_TEMPLATE = `opbi/ncm-preset-${config.component.type}`;
const template = config.component.template || DEFAULT_TEMPLATE;
await exec(`rm -rf .template`);
await exec(`git clone git@github.com:${template}.git .template`);
};

export const removeTemplateDir = async () => exec('rm -rf .template');
14 changes: 14 additions & 0 deletions src/actions/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as commonSteps from '../common-steps';
import * as steps from './steps';

export default async () => {
const config = await commonSteps.readConfig();

await commonSteps.cloneTemplateRepo(config);

await steps.copyConfigFiles();

await steps.updatePackageJson();

await commonSteps.removeTemplateDir();
};
40 changes: 40 additions & 0 deletions src/actions/config/steps.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import cpy from 'cpy';
import jsonfile from 'jsonfile';
import { exec } from 'lib/child-process';
import { sortObjectByKeys } from 'lib/util';

/*
copy only config files
*/
export const copyConfigFiles = async () => {
await cpy(
[
'.template/*',
'.template/.*',
'!.template/.ncmrc.yml', // there can be .ncmrc.yml in template
'!.template/*.md',
'!.template/package.json',
'!.template/yarn.lock',
],
'.',
);
await exec('cp -r .template/.circleci .');
};

/**
* merge devDependencies and optionalDependencies from template package.json
*/
export const updatePackageJson = async () => {
const template = await jsonfile.readFile('.template/package.json');
const target = await jsonfile.readFile('./package.json');
const updated = Object.assign({}, target);
updated.devDependencies = sortObjectByKeys({
...target.devDependencies,
...template.devDependencies,
});
updated.optionalDependencies = sortObjectByKeys({
...target.optionalDependencies,
...template.optionalDependencies,
});
await jsonfile.writeFile('./package.json', updated, { spaces: 2 });
};
55 changes: 0 additions & 55 deletions src/actions/dotfiles.js

This file was deleted.

7 changes: 4 additions & 3 deletions src/actions/setup/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import * as commonSteps from '../common-steps';
import * as steps from './steps';

export default async () => {
const config = await steps.readConfig();
const config = await commonSteps.readConfig();

await steps.cloneTemplateRepo(config);
await commonSteps.cloneTemplateRepo(config);

await steps.copyTemplateFiles();

await steps.removeTemplateDir();
await commonSteps.removeTemplateDir();

await steps.updatePackageJson(config);

Expand Down
27 changes: 0 additions & 27 deletions src/actions/setup/steps.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cpy from 'cpy';
import cosmiconfig from 'cosmiconfig';
import jsonfile from 'jsonfile';
import replace from 'replace-in-file';

Expand All @@ -8,20 +7,6 @@ import { exec } from 'lib/child-process';

import configPackageJsonFromTemplate from 'lib/package-json';

// TODO: setup schema and sanitise config file
export const readConfig = async () => {
const MODULE_NAME = 'ncm';
const { config } = await cosmiconfig(MODULE_NAME).search();
return config;
};

export const cloneTemplateRepo = async config => {
const DEFAULT_TEMPLATE = `opbi/ncm-preset-${config.component.type}`;
const template = config.component.template || DEFAULT_TEMPLATE;
await exec(`rm -rf .template`);
await exec(`git clone git@github.com:${template}.git .template`);
};

export const copyTemplateFiles = async () => {
await cpy(
[
Expand All @@ -39,8 +24,6 @@ export const copyTemplateFiles = async () => {
await exec('cp -r .template/.circleci .');
};

export const removeTemplateDir = async () => exec('rm -rf .template');

export const updatePackageJson = async config => {
const PACKAGE_JSON_PATH = './package.json';
const template = await jsonfile.readFile(PACKAGE_JSON_PATH);
Expand Down Expand Up @@ -100,14 +83,4 @@ export const commitAndPushToGitHub = async () => {
await exec('git push -u origin master');
};

// export const setupCIPipeline = async config => {};

// export const setupCoveralls = async config => {};

// export const setupScrutinizer = async config => {};

// export const initGitCommit = async () => {};

// export const installDeps = async () => {};

// export const initGitPush = async () => {};
15 changes: 8 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cli from 'commander';

import initAction from 'actions/init';
import setupAction from 'actions/setup';
import dotfilesAction from 'actions/dotfiles';
import configAction from 'actions/config';
import dotenvAction from 'actions/dotenv';

import packageJson from '../package';
Expand All @@ -22,17 +22,18 @@ cli
.action(setupAction);

cli
.command('dotfiles')
.alias('dotfile')
.alias('config')
.alias('.config')
.description('generate dotfiles based on template and specs in .ncmrc.yml')
.action(dotfilesAction);
.command('config')
.option('-e, --environment [envID]', 'dev, ci')
.description(
'generate config files based on template and specs in .ncmrc.yml',
)
.action(configAction);

cli
.command('dotenv')
.alias('env')
.alias('.env')
.alias('secret')
.description('write vault dev secrets to .env')
.option('-s, --scope <scope>', 'path of the secret in vault')
.option('-e --endpoint [vaultEndpoint]', 'endpoint of the vault server')
Expand Down

0 comments on commit a44b3f3

Please sign in to comment.