Skip to content

Commit

Permalink
refactor: migrate to ESM (nodejs#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
targos authored Jan 18, 2022
1 parent bdfbb6e commit 919ec3b
Show file tree
Hide file tree
Showing 107 changed files with 1,025 additions and 1,386 deletions.
5 changes: 1 addition & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"node": true
},
"parserOptions": {
"ecmaFeatures": {
"globalReturn": true
},
"sourceType": "script"
"sourceType": "module"
}
}
3 changes: 0 additions & 3 deletions .mocharc.json

This file was deleted.

10 changes: 0 additions & 10 deletions bin/get-metadata

This file was deleted.

11 changes: 11 additions & 0 deletions bin/get-metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

import { fileURLToPath } from 'node:url';

import { runAsync } from '../lib/run.js';
import { setVerbosityFromEnv } from '../lib/verbosity.js';

setVerbosityFromEnv();

const script = fileURLToPath(new URL('git-node.js', import.meta.url));
runAsync(process.execPath, [script, 'metadata', ...process.argv.slice(2)]);
22 changes: 0 additions & 22 deletions bin/git-node

This file was deleted.

29 changes: 29 additions & 0 deletions bin/git-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env node

import { readdirSync } from 'fs';

import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import epilogue from '../components/git/epilogue.js';
import { setVerbosityFromEnv } from '../lib/verbosity.js';

setVerbosityFromEnv();

const commandFiles = readdirSync(new URL('../components/git', import.meta.url))
.filter(file => file !== 'epilogue.js');

function importCommand(commandFile) {
return import(new URL(`../components/git/${commandFile}`, import.meta.url));
}

Promise.all(commandFiles.map(importCommand)).then((commands) => {
const args = yargs(hideBin(process.argv));
commands.forEach(command => args.command(command));
args.command('help', false, () => {}, (yargs) => { yargs.showHelp(); })
.demandCommand(1)
.strict()
.epilogue(epilogue)
.help('help')
.parse();
});
83 changes: 41 additions & 42 deletions bin/ncu-ci → bin/ncu-ci.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
#!/usr/bin/env node

'use strict';
import yargs from 'yargs';
import clipboardy from 'clipboardy';

const {
import {
JobParser,
parseJobFromURL,
CI_TYPES_KEYS: {
PR,
COMMIT,
BENCHMARK,
CITGM,
CITGM_NOBUILD,
DAILY_MASTER
}
} = require('../lib/ci/ci_type_parser');
const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

const { listBuilds } = require('../lib/ci/ci_utils');

const { jobCache } = require('../lib/ci/build-types/job');
const { PRBuild } = require('../lib/ci/build-types/pr_build');
const { CommitBuild } = require('../lib/ci/build-types/commit_build');
const { DailyBuild } = require('../lib/ci/build-types/daily_build');
const { FailureAggregator } = require('../lib/ci/failure_aggregator');
const { BenchmarkRun } = require('../lib/ci/build-types/benchmark_run');
const { HealthBuild } = require('../lib/ci/build-types/health_build');
const { CITGMBuild } = require('../lib/ci/build-types/citgm_build');
const {
CI_TYPES_KEYS
} from '../lib/ci/ci_type_parser.js';
import { setVerbosityFromEnv } from '../lib/verbosity.js';
import { listBuilds } from '../lib/ci/ci_utils.js';
import { jobCache } from '../lib/ci/build-types/job.js';
import { PRBuild } from '../lib/ci/build-types/pr_build.js';
import { CommitBuild } from '../lib/ci/build-types/commit_build.js';
import { DailyBuild } from '../lib/ci/build-types/daily_build.js';
import { FailureAggregator } from '../lib/ci/failure_aggregator.js';
import { BenchmarkRun } from '../lib/ci/build-types/benchmark_run.js';
import { HealthBuild } from '../lib/ci/build-types/health_build.js';
import { CITGMBuild } from '../lib/ci/build-types/citgm_build.js';
import {
CITGMComparisonBuild
} = require('../lib/ci/build-types/citgm_comparison_build');

const {
} from '../lib/ci/build-types/citgm_comparison_build.js';
import {
RunPRJob
} = require('../lib/ci/run_ci');
const clipboardy = require('clipboardy');
const { writeJson, writeFile } = require('../lib/file');
const { getMergedConfig } = require('../lib/config');
} from '../lib/ci/run_ci.js';
import { writeJson, writeFile } from '../lib/file.js';
import { getMergedConfig } from '../lib/config.js';
import { runPromise } from '../lib/run.js';
import auth from '../lib/auth.js';
import Request from '../lib/request.js';
import CLI from '../lib/cli.js';
import { hideBin } from 'yargs/helpers';

setVerbosityFromEnv();

const { runPromise } = require('../lib/run');
const auth = require('../lib/auth');
const Request = require('../lib/request');
const CLI = require('../lib/cli');
const yargs = require('yargs');
const {
PR,
COMMIT,
BENCHMARK,
CITGM,
CITGM_NOBUILD,
DAILY_MASTER
} = CI_TYPES_KEYS;

const commandKeys = [
'rate',
Expand All @@ -53,8 +52,7 @@ const commandKeys = [
'citgm'
];

// eslint-disable-next-line no-unused-vars
const argv = yargs
const args = yargs(hideBin(process.argv))
.command({
command: 'rate <type>',
desc: 'Calculate the green rate of a CI job in the last 100 runs',
Expand Down Expand Up @@ -223,8 +221,9 @@ const argv = yargs
}
return true;
})
.help()
.argv;
.help();

args.parse();

const commandToType = {
commit: COMMIT,
Expand Down Expand Up @@ -564,7 +563,7 @@ async function main(command, argv) {
break;
}
default:
return yargs.showHelp();
return args.showHelp();
}

await commandHandler.initialize();
Expand Down
19 changes: 10 additions & 9 deletions bin/ncu-config → bin/ncu-config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/usr/bin/env node

'use strict';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

const {
import {
getConfig, updateConfig, GLOBAL_CONFIG, PROJECT_CONFIG, LOCAL_CONFIG
} = require('../lib/config');
} from '../lib/config.js';
import { setVerbosityFromEnv } from '../lib/verbosity.js';

const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

const yargs = require('yargs');
const argv = yargs
const args = yargs(hideBin(process.argv))
.command({
command: 'set <key> <value>',
desc: 'Set a config variable',
Expand Down Expand Up @@ -58,8 +58,9 @@ const argv = yargs
describe: 'Use project config (./.ncurc)'
})
.conflicts('global', 'project')
.help()
.argv;
.help();

const argv = args.parse();

function getConfigType(argv) {
if (argv.global) {
Expand Down Expand Up @@ -95,5 +96,5 @@ function listHandler(argv) {
}

if (!['get', 'set', 'list'].includes(argv._[0])) {
yargs.showHelp();
args.showHelp();
}
54 changes: 28 additions & 26 deletions bin/ncu-team → bin/ncu-team.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
#!/usr/bin/env node

'use strict';
const Request = require('../lib/request');
const auth = require('../lib/auth');
const { runPromise } = require('../lib/run');
const CLI = require('../lib/cli');
const TeamInfo = require('../lib/team_info');
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

import Request from '../lib/request.js';
import auth from '../lib/auth.js';
import { runPromise } from '../lib/run.js';
import CLI from '../lib/cli.js';
import TeamInfo from '../lib/team_info.js';

import { setVerbosityFromEnv } from '../lib/verbosity.js';

const { setVerbosityFromEnv } = require('../lib/verbosity');
setVerbosityFromEnv();

require('yargs') // eslint-disable-line
.command({
command: 'list <team> [org]',
desc: 'Get the list of members in a team',
builder: (yargs) => {
yargs
.positional('team', {
describe: 'Name of the team',
type: 'string'
})
.positional('org', {
describe: 'Name of the organization',
type: 'string',
default: 'nodejs'
});
},
handler: handler
})
yargs(hideBin(process.argv)).command({
command: 'list <team> [org]',
desc: 'Get the list of members in a team',
builder: (yargs) => {
yargs
.positional('team', {
describe: 'Name of the team',
type: 'string'
})
.positional('org', {
describe: 'Name of the organization',
type: 'string',
default: 'nodejs'
});
},
handler: handler
})
.command({
command: 'sync <file>',
desc:
Expand All @@ -43,7 +45,7 @@ require('yargs') // eslint-disable-line
})
.demandCommand(1, 'must provide a valid command')
.help()
.argv;
.parse();

function handler(argv) {
runPromise(main(argv));
Expand Down
36 changes: 16 additions & 20 deletions components/git/backport.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
import { parsePRFromURL } from '../../lib/links.js';
import CLI from '../../lib/cli.js';
import { runPromise } from '../../lib/run.js';
import BackportSession from '../../lib/backport_session.js';

const yargs = require('yargs');
const { parsePRFromURL } = require('../../lib/links');
const CLI = require('../../lib/cli');
const { runPromise } = require('../../lib/run');
const BackportSession = require('../../lib/backport_session');
export const command = 'backport <identifier>';
export const describe = 'Backport a PR to a release staging branch.';

const epilogue = `====================== Example =======================
Demo: https://asciinema.org/a/221244
Expand All @@ -23,7 +23,10 @@ $ git node backport 24816 --to 11
=====================================================
`;

function builder(yargs) {
let yargsInstance;

export function builder(yargs) {
yargsInstance = yargs;
return yargs
.options({
to: {
Expand All @@ -40,7 +43,9 @@ function builder(yargs) {
.wrap(90);
}

async function main(config) {
async function main(argv, parsed) {
const merged = (await import('../../lib/config.js')).getMergedConfig();
const config = Object.assign({}, argv, parsed, merged);
const logStream = process.stdout.isTTY ? process.stdout : process.stderr;
const cli = new CLI(logStream);
cli.setFigureIndent(0);
Expand All @@ -49,26 +54,17 @@ async function main(config) {
return session.backport();
}

function handler(argv) {
export function handler(argv) {
let parsed = {};
const prid = Number.parseInt(argv.identifier);
if (!Number.isNaN(prid)) {
parsed.prid = prid;
} else {
parsed = parsePRFromURL(argv.identifier);
if (!parsed) {
return yargs.showHelp();
return yargsInstance.showHelp();
}
}

const config = require('../../lib/config').getMergedConfig();
const merged = Object.assign({}, argv, parsed, config);
return runPromise(main(merged));
return runPromise(main(argv, parsed));
}

module.exports = {
command: 'backport <identifier>',
describe: 'Backport a PR to a release staging branch.',
builder,
handler
};
4 changes: 1 addition & 3 deletions components/git/epilogue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports = `Steps to land a pull request:
export default `Steps to land a pull request:
==============================================================================
$ cd path/to/node/project
Expand Down
Loading

0 comments on commit 919ec3b

Please sign in to comment.