-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bob): support custom target definitions (#732)
### Summary This adds the `custom` target to bob. Users are able to pass arbitrary scripts via this target and bob will call those scripts. ### Test plan 1. Configure bob in a new project 2. Define the `custom` target 3. Define a script that generates some files 4. Call bob build and make sure the script was called with the right package manager --------- Co-authored-by: Satyajit Sahoo <[email protected]>
- Loading branch information
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import kleur from 'kleur'; | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import type { Input } from '../types'; | ||
import { spawn } from '../utils/spawn'; | ||
import dedent from 'dedent'; | ||
import del from 'del'; | ||
|
||
type Options = Omit<Input, 'output'> & { | ||
options?: { | ||
script?: string; | ||
clean?: string; | ||
}; | ||
}; | ||
|
||
export default async function customTarget({ options, root, report }: Options) { | ||
if (options?.script == null) { | ||
report.error( | ||
dedent( | ||
`No script was provided with the custom target. | ||
Example: ${kleur.green('{["custom", { "script": "generateTypes" }}')}` | ||
) | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const pathToClean = options.clean | ||
? path.relative(root, options.clean) | ||
: undefined; | ||
|
||
if (pathToClean) { | ||
report.info(`Cleaning up ${kleur.blue(pathToClean)}`); | ||
|
||
await del([path.resolve(root, pathToClean)]); | ||
} | ||
|
||
const packageManagerExecutable = process.env.npm_execpath ?? 'npm'; | ||
const packageManagerArgs = ['run', options.script]; | ||
|
||
// usr/bin/yarn -> yarn | ||
const packageManagerName = path.basename(packageManagerExecutable); | ||
report.info( | ||
`Running ${kleur.blue(packageManagerName)} ${kleur.blue( | ||
packageManagerArgs.join(' ') | ||
)}` | ||
); | ||
|
||
try { | ||
await spawn(packageManagerExecutable, packageManagerArgs, { | ||
stdio: ['ignore', 'ignore', 'inherit'], | ||
}); | ||
} catch (e) { | ||
report.error( | ||
`An error occurred when running ${kleur.blue(options.script)}` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
report.success(`Ran the ${kleur.blue(options.script)} script succesfully`); | ||
|
||
if (options.clean && pathToClean && !(await fs.pathExists(pathToClean))) { | ||
report.warn( | ||
`Custom target with the ${kleur.blue( | ||
options.script | ||
)} script has ${kleur.blue(options.clean)} as the ${kleur.bold( | ||
'clean' | ||
)} option but this path wasn't created after running the script. Are you sure you've defined the ${kleur.bold( | ||
'clean' | ||
)} path correctly?` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters