Skip to content

Commit

Permalink
feat: specifying allowed build list via a json file (#13)
Browse files Browse the repository at this point in the history
* feat: specifying allowed build list via a json file

* bump

ref pnpm/pnpm#7167
  • Loading branch information
zkochan committed Oct 7, 2023
1 parent 3f6cec4 commit 04748e1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
13 changes: 11 additions & 2 deletions .bitmap
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,67 @@

{
"agent": {
"name": "agent",
"scope": "pnpm.network",
"version": "0.1.0",
"mainFile": "index.ts",
"rootDir": "network/agent"
},
"ca-file": {
"name": "ca-file",
"scope": "pnpm.network",
"version": "1.0.2",
"mainFile": "index.ts",
"rootDir": "network/ca-file"
},
"env-replace": {
"name": "env-replace",
"scope": "pnpm.config",
"version": "1.1.0",
"mainFile": "index.ts",
"rootDir": "config/env-replace"
},
"env/path-extender": {
"name": "env/path-extender",
"scope": "pnpm.os",
"version": "0.2.11",
"mainFile": "index.ts",
"rootDir": "os/env/path-extender"
},
"env/path-extender-posix": {
"name": "env/path-extender-posix",
"scope": "pnpm.os",
"version": "0.2.9",
"mainFile": "index.ts",
"rootDir": "os/env/path-extender-posix"
},
"env/path-extender-windows": {
"name": "env/path-extender-windows",
"scope": "pnpm.os",
"version": "0.2.4",
"mainFile": "index.ts",
"rootDir": "os/env/path-extender-windows"
},
"lex-comparator": {
"name": "lex-comparator",
"scope": "pnpm.util",
"version": "1.0.0",
"mainFile": "index.ts",
"rootDir": "util/lex-comparator"
},
"policy": {
"name": "policy",
"scope": "pnpm.builder",
"version": "1.0.0",
"version": "1.1.0-1",
"mainFile": "index.ts",
"rootDir": "builder/policy"
},
"proxy-agent": {
"name": "proxy-agent",
"scope": "pnpm.network",
"version": "0.1.0",
"mainFile": "index.ts",
"rootDir": "network/proxy-agent"
},
"$schema-version": "16.0.0"
"$schema-version": "17.0.0"
}
4 changes: 4 additions & 0 deletions builder/policy/onlyBuild.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"zoo",
"qar"
]
25 changes: 23 additions & 2 deletions builder/policy/policy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import path from 'path'
import { createAllowBuildFunction } from './policy'

it('should neverBuiltDependencies', () => {
Expand All @@ -6,15 +7,35 @@ it('should neverBuiltDependencies', () => {
})
expect(allowBuild('foo')).toBeFalsy()
expect(allowBuild('bar')).toBeTruthy()
});
})

it('should onlyBuiltDependencies', () => {
const allowBuild = createAllowBuildFunction({
onlyBuiltDependencies: ['foo'],
})
expect(allowBuild('foo')).toBeTruthy()
expect(allowBuild('bar')).toBeFalsy()
});
})

it('should onlyBuiltDependencies set via a file', () => {
const allowBuild = createAllowBuildFunction({
onlyBuiltDependenciesFile: path.join(__dirname, 'onlyBuild.json'),
})
expect(allowBuild('zoo')).toBeTruthy()
expect(allowBuild('qar')).toBeTruthy()
expect(allowBuild('bar')).toBeFalsy()
})

it('should onlyBuiltDependencies set via a file and config', () => {
const allowBuild = createAllowBuildFunction({
onlyBuiltDependencies: ['bar'],
onlyBuiltDependenciesFile: path.join(__dirname, 'onlyBuild.json'),
})
expect(allowBuild('zoo')).toBeTruthy()
expect(allowBuild('qar')).toBeTruthy()
expect(allowBuild('bar')).toBeTruthy()
expect(allowBuild('esbuild')).toBeFalsy()
})

it('should return undefined if no policy is set', () => {
expect(createAllowBuildFunction({})).toBeUndefined()
Expand Down
14 changes: 11 additions & 3 deletions builder/policy/policy.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import fs from 'fs'

export function createAllowBuildFunction (
opts: {
neverBuiltDependencies?: string[]
onlyBuiltDependencies?: string[]
onlyBuiltDependenciesFile?: string
}
): undefined | ((pkgName: string) => boolean) {
if (opts.onlyBuiltDependenciesFile || opts.onlyBuiltDependencies != null) {
const onlyBuiltDeps = opts.onlyBuiltDependencies ?? []
if (opts.onlyBuiltDependenciesFile) {
onlyBuiltDeps.push(...JSON.parse(fs.readFileSync(opts.onlyBuiltDependenciesFile, 'utf8')))
}
const onlyBuiltDependencies = new Set(onlyBuiltDeps)
return (pkgName) => onlyBuiltDependencies.has(pkgName)
}
if (opts.neverBuiltDependencies != null && opts.neverBuiltDependencies.length > 0) {
const neverBuiltDependencies = new Set(opts.neverBuiltDependencies)
return (pkgName) => !neverBuiltDependencies.has(pkgName)
} else if (opts.onlyBuiltDependencies != null) {
const onlyBuiltDependencies = new Set(opts.onlyBuiltDependencies)
return (pkgName) => onlyBuiltDependencies.has(pkgName)
}
return undefined
}

0 comments on commit 04748e1

Please sign in to comment.