Skip to content

Commit

Permalink
chore(build): Better rewrite scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
grantila committed Feb 11, 2023
1 parent 82bc60d commit c7ea842
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions scripts/file.ts
Original file line number Diff line number Diff line change
@@ -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( );
}
30 changes: 30 additions & 0 deletions scripts/rewrite-esm.ts
Original file line number Diff line number Diff line change
@@ -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 );
} );
13 changes: 9 additions & 4 deletions scripts/rewrite.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

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' );

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 );
} );

0 comments on commit c7ea842

Please sign in to comment.