Skip to content

Commit fc5d21c

Browse files
committed
chore(tools): migrate sync-proto to ESM
1 parent 983f3e1 commit fc5d21c

File tree

15 files changed

+195
-186
lines changed

15 files changed

+195
-186
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"tools:bump-versions": "node tools/bump-versions",
5050
"tools:infer-next-version": "node tools/infer-next-version",
5151
"tools:patch": "node ./tools/typedoc-patcher --no-stash",
52-
"tools:sync-proto": "node tools/sync-proto"
52+
"tools:sync-proto": "node tools/sync-proto.mjs"
5353
},
5454
"commitlint": {
5555
"extends": [

tools/sync-proto-modules/changelog.js renamed to tools/sync-proto-modules/changelog.mjs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
const { readFile, writeFile } = require( 'fs/promises' );
2-
const { resolve } = require( 'path' );
1+
import { readFile, writeFile } from 'fs/promises';
2+
import { resolve } from 'path';
3+
import { fileURLToPath } from 'url';
34

4-
const { isString, escapeRegExp } = require( 'lodash' );
5+
import _ from 'lodash';
56

6-
const { syncFile, tryReadFile } = require( './utils' );
7-
const { resolveRoot, spawn, captureStream } = require( '../utils' );
7+
import { syncFile, tryReadFile } from './utils/index.mjs';
8+
import { captureStream, resolveRoot, spawn } from '../utils.js';
89

910
const getOldestVersion = async pkgName => {
1011
try {
1112
const out = await spawn( 'npm', [ 'show', pkgName, 'versions', '--json' ], { stdio: [ null, captureStream(), captureStream() ], shell: true } );
1213
const versions = JSON.parse( out.stdout );
1314
return versions[0];
1415
} catch( e ){
15-
if( e.code === 1 && e.stderr.match( new RegExp( `404\\s+'${escapeRegExp( pkgName )}@latest' is not in this registry.` ) ) ){
16+
if( e.code === 1 && e.stderr.match( new RegExp( `404\\s+'${_.escapeRegExp( pkgName )}@latest' is not in this registry.` ) ) ){
1617
return null;
1718
}
1819
throw e;
@@ -48,41 +49,50 @@ class Changelog {
4849
this.checkOnly = checkOnly;
4950
}
5051

51-
async setup( _, projects ){
52+
async setup( _proto, projects ){
5253
return {
53-
changelog: ( await readFile( resolve( __dirname, '../../CHANGELOG.md' ), 'utf-8' ) ).split( '\n' ),
54+
changelog: ( await readFile(
55+
resolve(
56+
fileURLToPath( new URL( '.', import.meta.url ) ),
57+
'../../CHANGELOG.md' ),
58+
'utf-8' )
59+
).split( '\n' ),
5460
oldestVersionCache: await this._getVersionsCache( projects ),
5561
};
5662
}
5763

58-
async run( _, project, projects, ___, { changelog: fullChangelogStrs, oldestVersionCache } ){
64+
async run( _proto, project, projects, _handlers, { changelog: fullChangelogStr, oldestVersionCache } ){
5965
const { pkgName, pkgJson, id, path } = project;
6066
const packagesIds = projects.map( p => ( { isDep: p.pkgName in ( pkgJson.dependencies ?? {} ), ...p } ) );
61-
const pkgChangelogFull = fullChangelogStrs
67+
const pkgChangelogFull = fullChangelogStr
6268
.map( reformatChangelogLines( id, packagesIds ) )
63-
.filter( isString )
69+
.filter( _.isString )
6470
.join( '\n' )
6571
.replace( /^### .*(\n*)^(?=##)/gm, '' )
6672
.replace( /^(## .*)(\n*)^(?=## )/gm, '$1\n\n\nNo notable changes were done in this version.\n\n\n' )
6773
.replace( /\n{4,}/g, '\n\n\n' );
6874
const firstVersion = oldestVersionCache[pkgName];
6975
const pkgChangelogFromFirstVersion = firstVersion ?
7076
pkgChangelogFull.replace(
71-
new RegExp( `(\n## .*\\W?${escapeRegExp( firstVersion )}\\W.*)\n((?:.|\n)*?\n)(## (.|\n)*)$` ),
77+
new RegExp( `(\n## .*\\W?${_.escapeRegExp( firstVersion )}\\W.*)\n((?:.|\n)*?\n)(## (.|\n)*)$` ),
7278
'$1\n\n\n**First release**\n$2' ) :
7379
pkgChangelogFull;
7480
const changelogPath = resolveRoot( path, 'CHANGELOG.md' );
7581
await syncFile( this.checkOnly, changelogPath, `${pkgChangelogFromFirstVersion.trim() }\n` );
7682
}
7783

78-
tearDown( _, __, ___, { oldestVersionCache } ){
79-
return writeFile( resolve( __dirname, '.changelog-cache.json' ), JSON.stringify( oldestVersionCache, null, 4 ), 'utf-8' );
84+
tearDown( _proto, _projects, _handlers, { oldestVersionCache } ){
85+
return writeFile(
86+
resolve(
87+
fileURLToPath( new URL( '.', import.meta.url ) ),
88+
'.changelog-cache.json' ),
89+
JSON.stringify( oldestVersionCache, null, 4 ), 'utf-8' );
8090
}
8191

8292
async _getVersionsCache( projects ) {
8393
const prevVersionsCache = Object.assign(
8494
Object.fromEntries( projects.filter( p => p.pkgJson.private ).map( p => [ p.pkgName, '0.0.0' ] ) ),
85-
JSON.parse( await tryReadFile( resolve( __dirname, '.changelog-cache.json' ) ) ?? '{}' ) );
95+
JSON.parse( await tryReadFile( resolve( fileURLToPath( new URL( '.', import.meta.url ) ), '.changelog-cache.json' ) ) ?? '{}' ) );
8696
await Promise.all( projects.map( async ( { pkgJson, pkgName } ) => {
8797
if( pkgJson.changelogStartsAt ){
8898
prevVersionsCache[pkgName] = pkgJson.changelogStartsAt;
@@ -100,6 +110,6 @@ class Changelog {
100110

101111
/**
102112
* @param {boolean} checkOnly
103-
* @returns {import('./utils').ProtoHandler<{changelog: string[], oldestVersionCache: Record<string, string|undefined>>}
113+
* @returns {import('./utils/index.mjs').ProtoHandler<{changelog: string[], oldestVersionCache: Record<string, string|undefined>>}
104114
*/
105-
module.exports.changelog = async checkOnly => new Changelog( checkOnly );
115+
export const changelog = async checkOnly => new Changelog( checkOnly );

tools/sync-proto-modules/circleci.js renamed to tools/sync-proto-modules/circleci.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
const { readFile } = require( 'fs/promises' );
1+
import { readFile } from 'fs/promises';
22

3-
const { syncFile, postProcessYaml } = require( './utils' );
4-
const { resolveRoot } = require( '../utils' );
3+
import { postProcessYaml, syncFile } from './utils/index.mjs';
4+
import { resolveRoot } from '../utils.js';
55

66
/**
77
* @param {boolean} checkOnly
8-
* @returns {import('./utils').ProtoHandler}
8+
* @returns {import('./utils/index.mjs').ProtoHandler}
99
*/
10-
module.exports.circleCi = async checkOnly => ( {
10+
export const circleCi = async checkOnly => ( {
1111
tearDown: async() => {
1212
const circleCiPath = resolveRoot( '.circleci/config.yml' );
1313
const currentCircleCi = await readFile( circleCiPath, 'utf-8' );

tools/sync-proto-modules/issue-template.js renamed to tools/sync-proto-modules/issue-template.mjs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
const { readFile } = require( 'fs/promises' );
1+
import { readFile } from 'fs/promises';
22

3-
const { glob } = require( 'glob' );
4-
const { minVersion } = require( 'semver' );
3+
import { glob } from 'glob';
4+
import { minVersion } from 'semver';
5+
6+
import { postProcessYaml, syncFile } from './utils/index.mjs';
7+
import { resolveRoot } from '../utils.js';
58

6-
const { syncFile, postProcessYaml } = require( './utils' );
7-
const { resolveRoot } = require( '../utils' );
89
const DIR = resolveRoot( './.github/ISSUE_TEMPLATE' );
910

1011
/**
1112
* @param {boolean} checkOnly
12-
* @returns {import('./utils').ProtoHandler}
13+
* @returns {import('./utils/index.mjs').ProtoHandler}
1314
*/
14-
module.exports.issueTemplate = async checkOnly => ( {
15+
export const issueTemplate = async checkOnly => ( {
1516
tearDown: async ( _, projects ) => {
1617
await Promise.all( ( await glob( '*.yaml', { cwd: DIR } ) ).map( async f => {
1718
const path = resolveRoot( DIR, f );
1819
const yaml = await readFile( path, 'utf-8' );
1920
const yamlFormatted = postProcessYaml( yaml, {
2021
plugins: projects.filter( p => !p.pkgJson.private ).map( p => p.pkgName ),
21-
typedocVersion: minVersion( require( resolveRoot( 'package.json' ) ).devDependencies.typedoc ),
22+
typedocVersion: minVersion( JSON.parse( await readFile( resolveRoot( 'package.json' ), 'utf-8' ) ).devDependencies.typedoc ),
2223
} );
2324
await syncFile( checkOnly, path, yamlFormatted );
2425
} ) );

tools/sync-proto-modules/package-json.js renamed to tools/sync-proto-modules/package-json.mjs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
const { readFile } = require( 'fs/promises' );
2-
const { resolve } = require( 'path' );
1+
import { readFile } from 'fs/promises';
2+
import { resolve } from 'path';
33

4-
const { memoize, cloneDeep, defaultsDeep, uniq } = require( 'lodash' );
5-
const semver = require( 'semver' );
4+
import _ from 'lodash';
5+
import { minVersion, satisfies } from 'semver';
66

7-
const { syncFile, formatPackage, getDocsUrl } = require( './utils' );
8-
const { resolveRoot } = require( '../utils' );
7+
import { formatPackage, getDocsUrl, syncFile } from './utils/index.mjs';
8+
import { resolveRoot } from '../utils.js';
99

1010
/**
1111
* @param {boolean} checkOnly
12-
* @returns {import('./utils').ProtoHandler<{getProtoPkg: (v: string) => Promise<string>, rootJson: any, rootJsonStr: string, rootPath: string}}
12+
* @returns {import('./utils/index.mjs').ProtoHandler<{getProtoPkg: (v: string) => Promise<string>, rootJson: any, rootJsonStr: string, rootPath: string}}
1313
*/
14-
module.exports.packageJson = async checkOnly => ( {
14+
export const packageJson = async checkOnly => ( {
1515
setup: async () => {
16-
const getProtoPkg = memoize( proto => readFile( resolve( proto, 'package.json' ), 'utf-8' ) );
16+
const getProtoPkg = _.memoize( proto => readFile( resolve( proto, 'package.json' ), 'utf-8' ) );
1717
const rootPath = resolveRoot( 'package.json' );
1818
const rootJsonStr = await readFile( rootPath, 'utf-8' );
1919
const rootJson = JSON.parse( rootJsonStr );
2020
return { getProtoPkg, rootJson, rootJsonStr, rootPath };
2121
},
22-
run: async ( proto, { path: projectPath, pkgJsonPath, pkgJson }, projects, _, { getProtoPkg, rootJson: rootPackageJson } ) => {
22+
run: async ( proto, { path: projectPath, pkgJsonPath, pkgJson }, projects, _handlers, { getProtoPkg, rootJson: rootPackageJson } ) => {
2323
const protoPkgContent = await getProtoPkg( proto );
2424
const protoPkg = JSON.parse( protoPkgContent
2525
.replace( /\{projectRelDir\}/g, projectPath )
2626
.replace( /\{projectTypeDocUrl\}/g, getDocsUrl( pkgJson ) ) );
27-
const newProjectPkg = defaultsDeep( cloneDeep( protoPkg ), pkgJson );
28-
[ 'keywords', 'files' ].forEach( prop => newProjectPkg[prop] = uniq( [
27+
const newProjectPkg = _.defaultsDeep( _.cloneDeep( protoPkg ), pkgJson );
28+
[ 'keywords', 'files' ].forEach( prop => newProjectPkg[prop] = _.uniq( [
2929
...( protoPkg[prop] ?? [] ),
3030
...( pkgJson[prop] ?? [] ),
3131
]
@@ -43,7 +43,7 @@ module.exports.packageJson = async checkOnly => ( {
4343
if( projects.some( p => p.pkgName === depName ) ){
4444
return false;
4545
}
46-
if( k === 'peerDependencies' && semver.satisfies( semver.minVersion( rootPkgDeps[depName] ), depV ) ){
46+
if( k === 'peerDependencies' && satisfies( minVersion( rootPkgDeps[depName] ), depV ) ){
4747
return false;
4848
}
4949
if( depName in rootPkgDeps && rootPkgDeps[depName] !== depV ){
@@ -57,7 +57,7 @@ module.exports.packageJson = async checkOnly => ( {
5757
};
5858
} );
5959
},
60-
tearDown: async( proto, projects, _, { rootJson, rootPath } ) => {
60+
tearDown: async( proto, projects, _handlers, { rootJson, rootPath } ) => {
6161
rootJson['devDependencies'] = {
6262
...rootJson['devDependencies'],
6363
...Object.fromEntries( projects.map( p => [ p.pkgName, `file:${p.path}` ] ) ),

tools/sync-proto-modules/readme.js renamed to tools/sync-proto-modules/readme.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { yellow } = require( 'chalk' );
2-
const { glob } = require( 'glob' );
1+
import chalk from 'chalk';
2+
import { glob } from 'glob';
33

4-
const { tryReadFile, getDocsUrl, syncFile } = require( './utils' );
4+
import { getDocsUrl, syncFile, tryReadFile } from './utils/index.mjs';
55

66
class Readme {
77
constructor( checkOnly ){
@@ -55,7 +55,7 @@ ${newHeader}
5555
`;
5656
const headerRegex = /^<!-- HEADER -->(.*)<!-- HEADER end -->(\r?\n|$)/sm;
5757
if( !headerRegex.test( readmeContent ) ){
58-
console.log( yellow( `Header not found in ${readmeContent}` ) );
58+
console.log( chalk.yellow( `Header not found in ${readmeContent}` ) );
5959
}
6060
return newHeader + readmeContent.replace( headerRegex, '' );
6161
}
@@ -83,11 +83,11 @@ This plugin version should match TypeDoc \`${typedocVer}\` for compatibility.
8383
`;
8484
const installRegex = /^<!-- INSTALL -->(.*)<!-- INSTALL end -->(\r?\n|$)/sm;
8585
if( !installRegex.test( readmeContent ) ){
86-
console.log( yellow( `Install not found in ${readmeContent}` ) );
86+
console.log( chalk.yellow( `Install not found in ${readmeContent}` ) );
8787
return `${readmeContent }\n\n${newInstall}`;
8888
}
8989
return readmeContent.replace( installRegex, newInstall );
9090
}
9191
}
9292

93-
module.exports.readme = checkOnly => new Readme( checkOnly );
93+
export const readme = checkOnly => new Readme( checkOnly );

tools/sync-proto-modules/sync-fs.js renamed to tools/sync-proto-modules/sync-fs.mjs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
const assert = require( 'assert' );
2-
const { createHash } = require( 'crypto' );
3-
const { readFile, mkdir, access, copyFile, unlink } = require( 'fs/promises' );
1+
import assert, { equal, fail } from 'assert';
2+
import { createHash } from 'crypto';
3+
import { access, copyFile, mkdir, readFile, unlink } from 'fs/promises';
4+
import { join, resolve } from 'path';
5+
import { fileURLToPath } from 'url';
46

5-
const { resolve, join } = require( 'path' );
7+
import chalk from 'chalk';
8+
import { glob } from 'glob';
9+
import _ from 'lodash';
610

7-
const { bold } = require( 'chalk' );
8-
const { glob } = require( 'glob' );
9-
const { memoize, partition, isString } = require( 'lodash' );
10-
11-
const { syncFile, tryReadFile } = require( './utils' );
11+
import { syncFile, tryReadFile } from './utils/index.mjs';
1212

1313
const checksum = async file => createHash( 'md5' )
1414
.update( ( await readFile( file, 'utf-8' ) ).replace( /\r?\n/g, '\n' ), 'utf-8' )
1515
.digest( 'hex' );
1616

17-
const cacheFile = resolve( __dirname, '../.sync-proto-cache' );
18-
const readCache = memoize( async () => {
17+
const cacheFile = resolve( fileURLToPath( new URL( '.', import.meta.url ) ), '../.sync-proto-cache' );
18+
const readCache = _.memoize( async () => {
1919
try {
2020
const cacheContent = ( await tryReadFile( cacheFile, 'utf-8' ) ) ?? '';
2121
return cacheContent
@@ -36,14 +36,14 @@ const readCache = memoize( async () => {
3636
}
3737
} );
3838

39-
const protoFs = memoize( async ( proto, handlers ) => {
39+
const protoFs = _.memoize( async ( proto, handlers ) => {
4040
const filesDirs = ( await glob( '**', { cwd: proto, ignore: [ '**/node_modules/**' ], mark: true, dot: true } ) )
4141
.filter( fd => !( handlers.some( h => h.handleFile?.( fd ) ?? false ) ) );
42-
const [ dirs, files ] = partition( filesDirs, p => p.endsWith( '/' ) );
42+
const [ dirs, files ] = _.partition( filesDirs, p => p.endsWith( '/' ) );
4343
return { dirs, files };
4444
} );
4545

46-
const getChangedFiles = memoize( async ( proto, handlers ) => {
46+
const getChangedFiles = _.memoize( async ( proto, handlers ) => {
4747
const [ cacheContent, { files } ] = await Promise.all( [
4848
readCache(),
4949
protoFs( proto, handlers ),
@@ -65,9 +65,9 @@ const getChangedFiles = memoize( async ( proto, handlers ) => {
6565

6666
/**
6767
* @param {boolean} checkOnly
68-
* @returns {import('./utils').ProtoHandler}
68+
* @returns {import('./utils/index.mjs').ProtoHandler}
6969
*/
70-
module.exports.syncFs = checkOnly => {
70+
export const syncFs = checkOnly => {
7171
const conflicting = [];
7272
return {
7373
name: 'syncFs',
@@ -80,7 +80,7 @@ module.exports.syncFs = checkOnly => {
8080
const changedFiles = await getChangedFiles( proto, handlers );
8181
if( checkOnly ){
8282
const changedFilesNames = Object.keys( changedFiles );
83-
assert.equal( changedFilesNames.length, 0, `Some files has changed compared to prototype. ${changedFilesNames.join( ' ' )}` );
83+
equal( changedFilesNames.length, 0, `Some files has changed compared to prototype. ${changedFilesNames.join( ' ' )}` );
8484
} else {
8585
await Promise.all( Object.entries( changedFiles ).map( async ( [ file, protoSum ] ) => {
8686
const source = resolve( proto, file );
@@ -116,7 +116,7 @@ module.exports.syncFs = checkOnly => {
116116
await access( absFile );
117117
} catch( e ){
118118
if( checkOnly ){
119-
assert.fail( `Missing ${absFile}` );
119+
fail( `Missing ${absFile}` );
120120
}
121121
await copyFile( resolve( proto, file ), absFile );
122122
}
@@ -130,7 +130,7 @@ module.exports.syncFs = checkOnly => {
130130
return;
131131
}
132132
conflicting.forEach( c => {
133-
console.error( `File ${bold( c )} has been changed compared to prototype. Please review git changes.` );
133+
console.error( `File ${chalk.bold( c )} has been changed compared to prototype. Please review git changes.` );
134134
} );
135135
const [ cache, changed ] = await Promise.all( [
136136
readCache(),
@@ -141,7 +141,7 @@ module.exports.syncFs = checkOnly => {
141141
...changed,
142142
};
143143
await syncFile( checkOnly, cacheFile, Object.entries( newCache )
144-
.filter( ( [ , v ] ) => isString( v ) )
144+
.filter( ( [ , v ] ) => _.isString( v ) )
145145
.map( entry => entry.join( ' :: ' ) )
146146
.join( '\n' ) );
147147
},

tools/sync-proto-modules/typedoc-submodule.js renamed to tools/sync-proto-modules/typedoc-submodule.mjs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const { resolve } = require( 'path' );
1+
import { readFile } from 'fs/promises';
2+
import { resolve } from 'path';
23

3-
const { SemVer } = require( 'semver' );
4+
import { SemVer } from 'semver';
45

5-
const { syncFile } = require( './utils/diff' );
6-
const { formatPackage } = require( './utils/package-json' );
7-
const { resolveRoot } = require( '../utils' );
6+
import { syncFile } from './utils/diff.mjs';
7+
import { formatPackage } from './utils/package-json.mjs';
8+
import { resolveRoot } from '../utils.js';
89

910
/**
1011
* @param {boolean} checkOnly
@@ -27,15 +28,15 @@ const syncPkgTypedocVersion = ( checkOnly, version ) => async ( pkg, path ) => {
2728

2829
/**
2930
* @param {boolean} checkOnly
30-
* @returns {import('./utils').ProtoHandler<ReturnType<typeof syncPkgTypedocVersion>>}
31+
* @returns {import('./utils/index.mjs').ProtoHandler<ReturnType<typeof syncPkgTypedocVersion>>}
3132
*/
32-
module.exports.typedocSubmodule = async checkOnly => ( {
33+
export const typedocSubmodule = async checkOnly => ( {
3334
setup: async proto => {
34-
const submoduleDir = resolveRoot( 'typedoc' );
35-
const submoduleVersion = require( resolve( submoduleDir, 'package.json' ) ).version;
35+
const submodulePkgPath = resolveRoot( 'typedoc', 'package.json' );
36+
const submoduleVersion = JSON.parse( await readFile( submodulePkgPath, 'utf-8' ) ).version;
3637
const doSync = syncPkgTypedocVersion( checkOnly, new SemVer( submoduleVersion ) );
3738
const protoPkgPath = resolve( proto, 'package.json' );
38-
const protoPkgJson = require( protoPkgPath );
39+
const protoPkgJson = JSON.parse( await readFile( protoPkgPath, 'utf-8' ) );
3940
await doSync( protoPkgJson, protoPkgPath );
4041
return doSync;
4142
},

0 commit comments

Comments
 (0)