Skip to content

Commit

Permalink
collect release package files from build meta
Browse files Browse the repository at this point in the history
  • Loading branch information
filecage committed Jul 29, 2023
1 parent 0cd12e7 commit d9a9773
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
52 changes: 44 additions & 8 deletions bin/prepare-release.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import {resolve, dirname} from 'path';
import {resolve, dirname, parse, format} from 'path';
import {fileURLToPath} from 'url';
import fs from 'fs/promises';
import packageDefinition from '../package.json' assert { type: "json" };
import buildMeta from '../dist/metafile-esm.json' assert { type: "json" };

const [,,version] = process.argv;
if (version === undefined || !version.match(/^\d+.\d+.\d+$/)) {
console.log(`ERROR: Not a valid version '${version}'`);
process.exit(1);
}

// Fail if any of these conditions are not met
assert(version !== undefined && version.match(/^\d+.\d+.\d+$/), `ERROR: Not a valid version '${version}'`);
assert(packageDefinition.version === undefined, `ERROR: 'version' is already defined in package.json`);
assert(packageDefinition.files === undefined, `ERROR: 'files' is already defined in package.json`);

// Collect files that the build emitted
const files = [];
for (const file of Object.keys(buildMeta.outputs)) {
files.push(file);

const types = replaceExt(file, '.d.ts');
try {
await fs.access(buildAppPath(types));
files.push(types);
} catch {
// its ok, i got u
}
};

// Delete obsolete flags that only clutter the file
delete packageDefinition.scripts;
Expand All @@ -17,11 +33,31 @@ delete packageDefinition.devDependencies;
const releasePackageDefinition = {
name: packageDefinition.name,
version,
...packageDefinition
...packageDefinition,
files,
};


// Write file
const packageDefinitionFilePath = resolve(dirname(fileURLToPath(import.meta.url)), '../package.json');
const packageDefinitionFilePath = buildAppPath('/package.json');
await fs.writeFile(packageDefinitionFilePath, JSON.stringify(releasePackageDefinition, null, ' '));

console.log(`OK: Release ${version}`);
console.log(`OK: Release ${version}`);


function buildAppPath (path) {
return resolve(dirname(fileURLToPath(import.meta.url)), '../' + path);
}

function replaceExt (path, ext) {
const parts = parse(path);

return format({...parts, ext, base: undefined});
}

function assert (assertion, errorMessage) {
if (!!assertion !== true) {
console.log(errorMessage);
process.exit(1);
}
}
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
]
}
},
"files": [
"dist/parser.d.ts",
"dist/parser.js"
],
"scripts": {
"build": "tsup",
"lint": "eslint src/**",
Expand Down
1 change: 1 addition & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
clean: true,
format: 'esm',
dts: true,
metafile: true,
esbuildPlugins: [
{
name: 'add-local-imports-extension',
Expand Down

0 comments on commit d9a9773

Please sign in to comment.