Skip to content

Commit

Permalink
Change inline configuration format (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Feb 1, 2021
1 parent 536397e commit fef4ede
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
env: {}

# DO NOT EDIT BELOW - use `npx ghat fregante/ghatemplates/node --exclude jobs.Test`
# FILE GENERATED WITH: npx ghat fregante/ghatemplates/node
# SOURCE: https://github.com/fregante/ghatemplates
# OPTIONS: {"exclude":["jobs.Test"]}

name: CI
on:
Expand All @@ -22,4 +24,4 @@ jobs:
- name: install
run: npm ci || npm install
- name: build
run: npm run build --if-present
run: npm run build
1 change: 0 additions & 1 deletion bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ prog
.action(async (source, options) => {
normalizeFlagArray(options, 'exclude');
normalizeFlagArray(options, 'set');
options.argv = process.argv;

try {
await ghat(source, options);
Expand Down
22 changes: 19 additions & 3 deletions lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const yaml = require('js-yaml');
const degit = require('degitto');
const dotProp = require('dot-prop');
const {outdent} = require('outdent');
const shellEscape = require('shell-escape');
const splitOnFirst = require('split-on-first');

const getRepoUrl = require('./parse-repo.js');

class InputError extends Error {}

async function loadYamlFile(path) {
Expand Down Expand Up @@ -45,7 +46,7 @@ async function getWorkflows(directory) {
return findYamlFiles(directory, '.github/workflows');
}

async function ghat(source, {exclude, set, argv}) {
async function ghat(source, {exclude, set}) {
if (!source) {
throw new InputError('No source was specified');
}
Expand Down Expand Up @@ -94,6 +95,8 @@ async function ghat(source, {exclude, set, argv}) {
}

needsUpdate = true;
} else {
exclude = undefined;
}

if (set.length > 0) {
Expand All @@ -103,15 +106,28 @@ async function ghat(source, {exclude, set, argv}) {
}

needsUpdate = true;
} else {
set = undefined;
}

if (needsUpdate) {
remote.string = yaml.dump(remote.parsed, {noCompatMode: true});
}

const comments = [
`FILE GENERATED WITH: npx ghat ${source}`,
`SOURCE: ${getRepoUrl(source).url}`
];

if (exclude || set) {
comments.push(
`OPTIONS: ${JSON.stringify({exclude, set})}`
);
}

await fs.writeFile(localWorkflowPath, outdent`
${yaml.dump({env})}
# DO NOT EDIT BELOW, USE: npx ghat ${shellEscape(argv.slice(2))}
${comments.map(line => '# ' + line).join('\n')}
${await remote.string}`
);
Expand Down
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
"prepack": "npm run build",
"test": "npm run build && xo"
},
"dependencies": {},
"devDependencies": {
"@vercel/ncc": "^0.27.0",
"degitto": "^2.8.2",
"dot-prop": "^6.0.1",
"js-yaml": "^4.0.0",
"outdent": "^0.8.0",
"sade": "^1.7.4",
"shell-escape": "^0.2.0",
"split-on-first": "^2.0.1",
"xo": "^0.37.1"
}
Expand Down
49 changes: 49 additions & 0 deletions parse-repo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Extracted from degit
// TODO: Export from degitto instead
const supported = new Set(['github', 'gitlab', 'bitbucket', 'git.sr.ht']);

class DegitError extends Error {
constructor(message, options) {
super(message);
Object.assign(this, options);
}
}

module.exports = src => {
const match = /^(?:(?:https:\/\/)?([^:/]+\.[^:/]+)\/|git@([^:/]+)[:/]|([^/]+):)?([^/\s]+)\/([^/\s#]+)(?:((?:\/[^/\s#]+)+))?\/?(?:#(.+))?/.exec(
src
);
if (!match) {
throw new DegitError(`could not parse ${src}`, {
code: 'BAD_SRC'
});
}

const site = (match[1] || match[2] || match[3] || 'github').replace(
/\.(com|org)$/,
''
);
if (!supported.has(site)) {
throw new DegitError(
'degit supports GitHub, GitLab, Sourcehut and BitBucket',
{
code: 'UNSUPPORTED_HOST'
}
);
}

const user = match[4];
const name = match[5].replace(/\.git$/, '');
const subdir = match[6];
const ref = match[7] || 'HEAD';

const domain = `${site}.${
site === 'bitbucket' ? 'org' : (site === 'git.sr.ht' ? '' : 'com')
}`;
const url = `https://${domain}/${user}/${name}`;
const ssh = `git@${domain}:${user}/${name}`;

const mode = supported.has(site) ? 'tar' : 'git';

return {site, user, name, ref, url, ssh, subdir, mode};
};

0 comments on commit fef4ede

Please sign in to comment.