Skip to content

Commit

Permalink
feat(init): create .github/workflows/*.yml (#641)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jul 14, 2020
1 parent a6d7712 commit 8719e54
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
tags: ["**"]

jobs:
run:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ on:
branches: ["master"]
pull_request:
branches: ["**"]
tags: ["**"]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: ["10", "12", "14"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2.1.0
Expand Down
25 changes: 17 additions & 8 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ const originalPackage = require("../package.json");
const copy = promisify(fs.copyFile);
const write = promisify(fs.writeFile);
const read = promisify(fs.readFile);
const mkdir = promisify(fs.mkdir);

const packagePath = (...pathElements) => path.join(...[__dirname, "..", ...pathElements]);
const packagePath = (elem, ...elems) => path.join(...[__dirname, "..", elem, ...elems]);

// eslint-disable-next-line max-lines-per-function
const initCommand = (baseDir, logger) => {
const currentPath = (...pathElems) => path.join(...[baseDir, ...pathElems]);
const currentPath = (elem, ...elems) => path.join(...[baseDir, elem, ...elems]);

const readFile = (fileName) => read(currentPath(fileName), "utf8");

const copyFile = async (src, dest) => {
await mkdir(path.resolve(dest, ".."), { recursive: true });
await copy(src, dest);
logger(`${dest} was updated.`);
logger(`=> \`${path.relative(baseDir, dest)}\` was updated`);
};

const writeFile = async (fileName, fileContent) => {
const file = currentPath(fileName);
await write(file, `${fileContent}\n`);
logger(`${file} was updated.`);
logger(`=> \`${path.relative(baseDir, file)}\` was updated`);
};

return {
Expand Down Expand Up @@ -67,8 +69,8 @@ const initCommand = (baseDir, logger) => {
await writeFile("package.json", JSON.stringify(packageInfo, null, 2));
},

async writePackageFile(name) {
await copyFile(packagePath(name), currentPath(name));
async writePackageFile(name, ...names) {
await copyFile(packagePath(name, ...names), currentPath(name, ...names));
},
};
};
Expand All @@ -77,11 +79,18 @@ const defaultLogger = (msg) => process.stdout.write(`${msg}${EOL}`);

module.exports = async function init({ cwd = process.cwd(), logger = defaultLogger } = {}) {
const cmd = initCommand(cwd, logger);

await cmd.updatePackageFile();
await cmd.writePackageFile(".editorconfig");
await cmd.writePackageFile(".remarkignore");
await cmd.writePackageFile(".github", "workflows", "commitlint.yml");
await cmd.writePackageFile(".github", "workflows", "npm-audit-fix.yml");
await cmd.writePackageFile(".github", "workflows", "release.yml");
await cmd.writePackageFile(".github", "workflows", "test.yml");
};

module.exports.desc = `Setup npm project:
- Update 'package.json'
- Create '.editorconfig'`;
- Update \`package.json\`
- Create \`.editorconfig\`
- Create \`.remarkignore\`
- Create \`.github/workflows/*.yml\``;
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
"bin",
"lib",
".editorconfig",
".remarkignore"
".remarkignore",
".github/workflows/commitlint.yml",
".github/workflows/npm-audit-fix.yml",
".github/workflows/release.yml",
".github/workflows/test.yml"
],
"engines": {
"node": ">=10.17.0"
Expand Down
6 changes: 4 additions & 2 deletions test/help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ cli.js <command>
Commands:
cli.js init Setup npm project:
- Update 'package.json'
- Create '.editorconfig'
- Update \`package.json\`
- Create \`.editorconfig\`
- Create \`.remarkignore\`
- Create \`.github/workflows/*.yml\`
Options:
--help, -h Show help [boolean]
Expand Down
25 changes: 22 additions & 3 deletions test/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ test("init", (t) => {
t.end();
});

[".editorconfig", ".remarkignore"].forEach((file) => {
[
".editorconfig",
".remarkignore",
".github/workflows/commitlint.yml",
".github/workflows/npm-audit-fix.yml",
".github/workflows/release.yml",
".github/workflows/test.yml",
].forEach((file) => {
testInSandbox(`write "${file}"`, async (t, ctx) => {
await ctx.fixture("package-normal.json");
await init(ctx.initArgs);
t.ok(ctx.logMessage().includes("package.json was updated."));
t.ok(ctx.logMessage().includes("`package.json` was updated"));

const original = await ctx.readOrigFile(file);
const copy = await ctx.readWorkFile(file);
Expand All @@ -94,7 +101,19 @@ test("init", (t) => {
testInSandbox("End-to-End via CLI", async (t, ctx) => {
await ctx.fixture("package-normal.json");
const { stdout, stderr } = await exec("init", { cwd: ctx.initArgs.cwd });
t.ok(stdout.includes("package.json was updated."));
t.match(
stdout,
new RegExp( // eslint-disable-line prefer-regex-literals
`=> \`package.json\` was updated
=> \`.editorconfig\` was updated
=> \`.remarkignore\` was updated
=> \`.github/workflows/commitlint.yml\` was updated
=> \`.github/workflows/npm-audit-fix.yml\` was updated
=> \`.github/workflows/release.yml\` was updated
=> \`.github/workflows/test.yml\` was updated`,
"u"
)
);
t.is(stderr, "");
t.end();
});
Expand Down

0 comments on commit 8719e54

Please sign in to comment.