Skip to content

Commit

Permalink
build subpackages with a script
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed May 22, 2023
1 parent e5900c5 commit 741474e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ download-binaries/*

bin/*

# copied from the root directory
/packages/*/LICENSE
/packages/*/index.js
/packages/*/install.js
# generated by build scripts
/packages/*/package.json
# not relevant
/packages/*/package-lock.json
70 changes: 70 additions & 0 deletions build-packages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env node

const {join: pathJoin} = require('path')
const {cpSync, writeFileSync} = require('fs')
const basePkg = require('./package.json')

const PACKAGES = [
'ffmpeg-static',
'ffprobe-static',
]

const copyFileIntoPackage = (pkgName, filename) => {
const src = pathJoin(__dirname, filename)
const dest = pathJoin(__dirname, 'packages', pkgName, filename)
cpSync(src, dest, {
dereference: true, // dereference symlinks
preserveTimestamps: true,
})
}

const generatePackageJsonForPackages = (pkgName) => {
const tplPath = pathJoin(__dirname, 'packages', pkgName, 'package.template.json')
const tpl = require(tplPath)

const packageJson = {
...basePkg,

// remove fields
private: undefined,
workspaces: undefined,
...Object.fromEntries(
PACKAGES
// remove own entry
.filter(_pkgName => _pkgName !== pkgName)
// remove others by setting `undefined` as value
.map(_pkgName => [_pkgName, undefined])
),

main: 'index.js',
files: [
'index.js',
'install.js',
'example.js',
'types',
],
types: 'types/index.d.ts',
scripts: {
install: 'node install.js',
prepublishOnly: 'npm run install',
},
devDependencies: {
...basePkg.devDependencies,
eslint: undefined, // remove field
},

...tpl,
}

const dest = pathJoin(__dirname, 'packages', pkgName, 'package.json')
writeFileSync(dest, JSON.stringify(packageJson, null, '\t'))
}

for (const pkgName of PACKAGES) {
copyFileIntoPackage(pkgName, 'LICENSE')
copyFileIntoPackage(pkgName, 'index.js')
copyFileIntoPackage(pkgName, 'install.js')
generatePackageJsonForPackages(pkgName)

console.info(pkgName, '✔︎')
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"scripts": {
"test": "node test.js",
"lint": "eslint .",
"prepublishOnly": "npm run lint && npm install --workspaces && npm test"
"build": "./build-packages.js",
"prepublishOnly": "npm run lint && npm run build && npm install --workspaces && npm test"
},
"ffmpeg-static": {
"binary-path-env-var": "FFMPEG_BIN",
Expand Down

0 comments on commit 741474e

Please sign in to comment.