Skip to content

Commit 830b815

Browse files
committed
refactor!: migrate to ESM
BREAKING CHANGE: migration to ESM
1 parent e0019fd commit 830b815

30 files changed

+242
-231
lines changed

bin/cli.js renamed to bin/cli.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ if (process.version.match(/v(\d+)\./)[1] < 6) {
66
'commit-and-tag-version: Node v6 or greater is required. `commit-and-tag-version` did not run.',
77
);
88
} else {
9-
const standardVersion = require('../index');
10-
const cmdParser = require('../command');
11-
standardVersion(cmdParser.argv).catch(() => {
9+
const standardVersion = (await import('../index.mjs')).default;
10+
const cmdParser = (await import('../command.mjs')).default;
11+
await standardVersion(cmdParser.argv).catch((err) => {
12+
console.error(err);
1213
process.exit(1);
1314
});
1415
}

command.js renamed to command.mjs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const spec = require('conventional-changelog-config-spec');
2-
const { getConfiguration } = require('./lib/configuration');
3-
const defaults = require('./defaults');
1+
import spec from 'conventional-changelog-config-spec';
2+
import { getConfiguration } from './lib/configuration.mjs';
3+
import defaults from './defaults.mjs';
4+
import yargs from 'yargs';
45

5-
const yargs = require('yargs')
6+
const yargsObj = yargs()
67
.usage('Usage: $0 [options]')
78
.option('packageFiles', {
89
default: defaults.packageFiles,
@@ -150,17 +151,17 @@ const yargs = require('yargs')
150151
)
151152
.pkgConf('standard-version')
152153
.pkgConf('commit-and-tag-version')
153-
.config(getConfiguration())
154+
.config(await getConfiguration())
154155
.wrap(97);
155156

156157
Object.keys(spec.properties).forEach((propertyKey) => {
157158
const property = spec.properties[propertyKey];
158-
yargs.option(propertyKey, {
159+
yargsObj.option(propertyKey, {
159160
type: property.type,
160161
describe: property.description,
161162
default: defaults[propertyKey] ? defaults[propertyKey] : property.default,
162163
group: 'Preset Configuration:',
163164
});
164165
});
165166

166-
module.exports = yargs;
167+
export default yargsObj;

defaults.js renamed to defaults.mjs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
const spec = require('conventional-changelog-config-spec');
1+
import spec from 'conventional-changelog-config-spec';
2+
3+
let preset = import.meta.resolve('conventional-changelog-conventionalcommits');
4+
5+
// Workaround for limitation on `node:path.isAbsolute()` (can't handle file:// on absolute path)
6+
preset = preset.replace("file://", "")
7+
8+
// Workaround specific to Windows (removes etra slash at the beginning of absolute path)
9+
preset = preset.replace(/^(\/)([A-Z]:\/.*)$/, "$2")
210

311
const defaults = {
412
infile: 'CHANGELOG.md',
@@ -15,7 +23,7 @@ const defaults = {
1523
dryRun: false,
1624
tagForce: false,
1725
gitTagFallback: true,
18-
preset: require.resolve('conventional-changelog-conventionalcommits'),
26+
preset: preset,
1927
npmPublishHint: undefined,
2028
};
2129

@@ -41,4 +49,4 @@ defaults.bumpFiles = defaults.packageFiles.concat([
4149
'npm-shrinkwrap.json',
4250
]);
4351

44-
module.exports = defaults;
52+
export default defaults;

index.js renamed to index.mjs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const bump = require('./lib/lifecycles/bump');
2-
const changelog = require('./lib/lifecycles/changelog');
3-
const commit = require('./lib/lifecycles/commit');
4-
const fs = require('fs');
5-
const latestSemverTag = require('./lib/latest-semver-tag');
6-
const path = require('path');
7-
const printError = require('./lib/print-error');
8-
const tag = require('./lib/lifecycles/tag');
9-
const { resolveUpdaterObjectFromArgument } = require('./lib/updaters');
1+
import { readFileSync } from 'fs';
2+
import { resolve } from 'path';
3+
import bump from './lib/lifecycles/bump.mjs';
4+
import changelog, { START_OF_LAST_RELEASE_PATTERN } from './lib/lifecycles/changelog.mjs';
5+
import commit from './lib/lifecycles/commit.mjs';
6+
import latestSemverTag from './lib/latest-semver-tag.mjs';
7+
import printError from './lib/print-error.mjs';
8+
import tag from './lib/lifecycles/tag.mjs';
9+
import { resolveUpdaterObjectFromArgument } from './lib/updaters/index.mjs';
1010

11-
module.exports = async function standardVersion(argv) {
12-
const defaults = require('./defaults');
11+
export default async function standardVersion(argv) {
12+
const defaults = (await import('./defaults.mjs')).default;
1313
/**
1414
* `--message` (`-m`) support will be removed in the next major version.
1515
*/
@@ -39,10 +39,10 @@ module.exports = async function standardVersion(argv) {
3939

4040
if (
4141
argv.header &&
42-
argv.header.search(changelog.START_OF_LAST_RELEASE_PATTERN) !== -1
42+
argv.header.search(START_OF_LAST_RELEASE_PATTERN) !== -1
4343
) {
4444
throw Error(
45-
`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`,
45+
`custom changelog header must not match ${START_OF_LAST_RELEASE_PATTERN}`,
4646
);
4747
}
4848

@@ -56,11 +56,11 @@ module.exports = async function standardVersion(argv) {
5656
const args = Object.assign({}, defaults, argv);
5757
let pkg;
5858
for (const packageFile of args.packageFiles) {
59-
const updater = resolveUpdaterObjectFromArgument(packageFile);
59+
const updater = await resolveUpdaterObjectFromArgument(packageFile);
6060
if (!updater) return;
61-
const pkgPath = path.resolve(process.cwd(), updater.filename);
61+
const pkgPath = resolve(process.cwd(), updater.filename);
6262
try {
63-
const contents = fs.readFileSync(pkgPath, 'utf8');
63+
const contents = readFileSync(pkgPath, 'utf8');
6464
pkg = {
6565
version: updater.updater.readVersion(contents),
6666
private:
@@ -76,7 +76,7 @@ module.exports = async function standardVersion(argv) {
7676
}
7777
try {
7878
let version;
79-
if (pkg && pkg.version) {
79+
if (pkg?.version) {
8080
version = pkg.version;
8181
} else if (args.gitTagFallback) {
8282
version = await latestSemverTag(args.tagPrefix);
@@ -92,4 +92,4 @@ module.exports = async function standardVersion(argv) {
9292
printError(args, err.message);
9393
throw err;
9494
}
95-
};
95+
}

lib/checkpoint.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
const chalk = require('chalk');
2-
const figures = require('figures');
3-
const util = require('util');
1+
import util from 'util';
2+
import chalk from 'chalk';
3+
import figures from 'figures';
44

5-
module.exports = function (argv, msg, args, figure) {
5+
export default function (argv, msg, args, figure) {
66
const defaultFigure = argv.dryRun
77
? chalk.yellow(figures.tick)
88
: chalk.green(figures.tick);
99
if (!argv.silent) {
1010
console.info(
1111
(figure || defaultFigure) +
12-
' ' +
13-
util.format.apply(
14-
util,
15-
[msg].concat(
16-
args.map(function (arg) {
17-
return chalk.bold(arg);
18-
}),
19-
),
12+
' ' +
13+
util.format.apply(
14+
util,
15+
[msg].concat(
16+
args.map(function (arg) {
17+
return chalk.bold(arg);
18+
}),
2019
),
20+
),
2121
);
2222
}
23-
};
23+
}

lib/configuration.js renamed to lib/configuration.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const path = require('path');
2-
const findUp = require('find-up');
3-
const { readFileSync } = require('fs');
1+
import path from 'path';
2+
import findUp from 'find-up';
3+
import { readFileSync } from 'fs';
44

55
const CONFIGURATION_FILES = [
66
'.versionrc',
@@ -9,15 +9,15 @@ const CONFIGURATION_FILES = [
99
'.versionrc.js',
1010
];
1111

12-
module.exports.getConfiguration = function () {
12+
export async function getConfiguration() {
1313
let config = {};
1414
const configPath = findUp.sync(CONFIGURATION_FILES);
1515
if (!configPath) {
1616
return config;
1717
}
1818
const ext = path.extname(configPath);
1919
if (ext === '.js' || ext === '.cjs') {
20-
const jsConfiguration = require(configPath);
20+
const jsConfiguration = (await import(configPath));
2121
if (typeof jsConfiguration === 'function') {
2222
config = jsConfiguration();
2323
} else {
@@ -38,4 +38,4 @@ module.exports.getConfiguration = function () {
3838
}
3939

4040
return config;
41-
};
41+
}

lib/detect-package-manager.js renamed to lib/detect-package-manager.mjs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* modified to support only detecting lock file and not detecting global package manager
55
*/
66

7-
const { promises: fs } = require('fs');
8-
const { resolve } = require('path');
7+
import { promises as fs } from 'fs';
8+
import { resolve } from 'path';
99

1010
/**
1111
* Check if a path exists
@@ -39,15 +39,11 @@ function getTypeofLockFile(cwd = '.') {
3939
});
4040
}
4141

42-
const detectPMByLockFile = async (cwd) => {
42+
export const detectPMByLockFile = async (cwd) => {
4343
const type = await getTypeofLockFile(cwd);
4444
if (type) {
4545
return type;
4646
}
4747

4848
return 'npm';
4949
};
50-
51-
module.exports = {
52-
detectPMByLockFile,
53-
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function (rawMsg, newVersion) {
1+
export default function (rawMsg, newVersion) {
22
const message = String(rawMsg);
33
return message.replace(/{{currentTag}}/g, newVersion);
4-
};
4+
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const gitSemverTags = require('git-semver-tags');
2-
const semver = require('semver');
1+
import gitSemverTags from 'git-semver-tags';
2+
import { clean, rcompare } from 'semver';
33

4-
module.exports = function (tagPrefix = undefined) {
4+
export default function (tagPrefix = undefined) {
55
return new Promise((resolve, reject) => {
66
gitSemverTags({ tagPrefix }, function (err, tags) {
77
if (err) return reject(err);
@@ -10,10 +10,10 @@ module.exports = function (tagPrefix = undefined) {
1010
tags = tags.map((tag) => tag.replace(new RegExp('^' + tagPrefix), ''));
1111
// ensure that the largest semver tag is at the head.
1212
tags = tags.map((tag) => {
13-
return semver.clean(tag);
13+
return clean(tag);
1414
});
15-
tags.sort(semver.rcompare);
15+
tags.sort(rcompare);
1616
return resolve(tags[0]);
1717
});
1818
});
19-
};
19+
}

lib/lifecycles/bump.js renamed to lib/lifecycles/bump.mjs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
'use strict';
1+
import fs from 'fs';
2+
import path from 'path';
3+
import semver from 'semver';
4+
import chalk from 'chalk';
5+
import conventionalRecommendedBump from 'conventional-recommended-bump';
6+
import figures from 'figures';
7+
import DotGitignore from 'dotgitignore';
8+
import checkpoint from '../checkpoint.mjs';
9+
import presetLoader from '../preset-loader.mjs';
10+
import runLifecycleScript from '../run-lifecycle-script.mjs';
11+
import writeFile from '../write-file.mjs';
12+
import { resolveUpdaterObjectFromArgument } from '../updaters/index.mjs';
213

3-
const chalk = require('chalk');
4-
const checkpoint = require('../checkpoint');
5-
const conventionalRecommendedBump = require('conventional-recommended-bump');
6-
const figures = require('figures');
7-
const fs = require('fs');
8-
const DotGitignore = require('dotgitignore');
9-
const path = require('path');
10-
const presetLoader = require('../preset-loader');
11-
const runLifecycleScript = require('../run-lifecycle-script');
12-
const semver = require('semver');
13-
const writeFile = require('../write-file');
14-
const { resolveUpdaterObjectFromArgument } = require('../updaters');
1514
let configsToUpdate = {};
1615
const sanitizeQuotesRegex = /['"]+/g;
1716

@@ -83,7 +82,7 @@ async function Bump(args, version) {
8382

8483
newVersion = semver.inc(version, releaseType, args.prerelease);
8584
}
86-
updateConfigs(args, newVersion);
85+
await updateConfigs(args, newVersion);
8786
} else {
8887
checkpoint(
8988
args,
@@ -116,7 +115,7 @@ function getReleaseType(prerelease, expectedReleaseType, currentVersion) {
116115
if (
117116
shouldContinuePrerelease(currentVersion, expectedReleaseType) ||
118117
getTypePriority(getCurrentActiveType(currentVersion)) >
119-
getTypePriority(expectedReleaseType)
118+
getTypePriority(expectedReleaseType)
120119
) {
121120
return 'prerelease';
122121
}
@@ -196,11 +195,11 @@ function bumpVersion(releaseAs, currentVersion, args) {
196195
lernaPackage: args.lernaPackage,
197196
...(args.verbose
198197
? {
199-
debug: console.info.bind(
200-
console,
201-
'conventional-recommended-bump',
202-
),
203-
}
198+
debug: console.info.bind(
199+
console,
200+
'conventional-recommended-bump',
201+
),
202+
}
204203
: {}),
205204
},
206205
args.parserOpts,
@@ -219,28 +218,28 @@ function bumpVersion(releaseAs, currentVersion, args) {
219218
* @param newVersion version number to update to.
220219
* @return void
221220
*/
222-
function updateConfigs(args, newVersion) {
221+
async function updateConfigs(args, newVersion) {
223222
const dotgit = DotGitignore();
224-
args.bumpFiles.forEach(function (bumpFile) {
225-
const updater = resolveUpdaterObjectFromArgument(bumpFile);
223+
for (const bumpFile of args.bumpFiles) {
224+
const updater = await resolveUpdaterObjectFromArgument(bumpFile);
226225
if (!updater) {
227-
return;
226+
continue;
228227
}
229228
const configPath = path.resolve(process.cwd(), updater.filename);
230229
try {
231230
if (dotgit.ignore(updater.filename)) {
232231
console.debug(
233232
`Not updating file '${updater.filename}', as it is ignored in Git`,
234233
);
235-
return;
234+
continue;
236235
}
237236
const stat = fs.lstatSync(configPath);
238237

239238
if (!stat.isFile()) {
240239
console.debug(
241240
`Not updating '${updater.filename}', as it is not a file`,
242241
);
243-
return;
242+
continue;
244243
}
245244
const contents = fs.readFileSync(configPath, 'utf8');
246245
const newContents = updater.updater.writeVersion(contents, newVersion);
@@ -257,7 +256,7 @@ function updateConfigs(args, newVersion) {
257256
} catch (err) {
258257
if (err.code !== 'ENOENT') console.warn(err.message);
259258
}
260-
});
259+
}
261260
}
262261

263-
module.exports = Bump;
262+
export default Bump;

0 commit comments

Comments
 (0)