diff --git a/package.json b/package.json index 4d9fa51..625bdbe 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "build:libphonenumber": "node_modules/.bin/gulp", "build:rewrite": "ts-node scripts/rewrite.ts", "build:esm:build": "rollup -c ./rollup.config.mjs", - "build:esm:rewrite": "cat build/index-esm.js src/esm-outro.js > index-esm.mjs", + "build:esm:rewrite": "ts-node scripts/rewrite-esm.ts", "build:esm": "yarn build:esm:build && yarn build:esm:rewrite", "build": "yarn build:libphonenumber && yarn build:rewrite && yarn build:esm", "test:compile": "scripts/test.sh", diff --git a/scripts/file.ts b/scripts/file.ts new file mode 100644 index 0000000..a4f6559 --- /dev/null +++ b/scripts/file.ts @@ -0,0 +1,9 @@ +import { promises } from 'fs' + +export async function overwriteFile( filename: string, data: string ) +{ + const file = await promises.open( filename, 'w' ); + await file.write( data, 0, 'utf-8' ); + await file.sync( ); + await file.close( ); +} diff --git a/scripts/rewrite-esm.ts b/scripts/rewrite-esm.ts new file mode 100644 index 0000000..ae2ae6b --- /dev/null +++ b/scripts/rewrite-esm.ts @@ -0,0 +1,30 @@ +import { promises } from 'fs' +import * as path from 'path' + +import { overwriteFile } from './file' + +const { readFile } = promises; + +const esmFile = path.resolve( __dirname, '..', 'build', 'index-esm.js' ); +const outroFile = path.resolve( __dirname, '..', 'src', 'esm-outro.js' ); +const resultFile = path.resolve( __dirname, '..', 'index-esm.mjs' ); + +async function rewrite( ) +{ + const [ file1, file2 ] = await Promise.all( [ + readFile( esmFile, 'utf-8' ), + readFile( outroFile, 'utf-8' ), + ] ); + + console.log( + `Lines: ${file1.split('\n').length} ${file2.split('\n').length}` + ); + const data = file1 + file2; + + await overwriteFile( resultFile, data ); +} +rewrite( ).catch( err => +{ + console.error( err.stack ); + process.exit( 1 ); +} ); diff --git a/scripts/rewrite.ts b/scripts/rewrite.ts index c35c250..16a65e6 100644 --- a/scripts/rewrite.ts +++ b/scripts/rewrite.ts @@ -1,8 +1,9 @@ - import { promises } from 'fs' import * as path from 'path' -const { readFile, writeFile } = promises; +import { overwriteFile } from './file' + +const { readFile } = promises; const distFile = path.resolve( __dirname, '..', 'lib', 'index.js' ); @@ -10,6 +11,10 @@ async function rewrite( ) { const data = await readFile( distFile, 'utf-8' ); const res = data.replace( /\bconst /g, 'var ' ); - await writeFile( distFile, res, 'utf-8' ); + await overwriteFile( distFile, res ); } -rewrite( ); +rewrite( ).catch( err => +{ + console.error( err.stack ); + process.exit( 1 ); +} );