forked from sanity-io/sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecScript.js
35 lines (29 loc) · 1.32 KB
/
execScript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const spawn = require('child_process').spawn
const path = require('path')
const dotenv = require('dotenv')
const fse = require('fs-extra')
// Try to load .env files from the current directory
// eslint-disable-next-line no-process-env
const env = process.env.SANITY_ACTIVE_ENV || process.env.NODE_ENV || 'development'
dotenv.config({path: path.join(process.cwd(), `.env.${env}`)})
module.exports = async (args) => {
// In case of specifying --with-user-token <file.js>, use the "token" as the script
const script = args.argsWithoutOptions[0] || args.extOptions['with-user-token']
const withToken = Boolean(args.extOptions['with-user-token'])
const scriptPath = path.resolve(script)
if (!script) {
throw new Error('SCRIPT must be provided. `sanity exec <script>`')
}
if (!(await fse.exists(scriptPath))) {
throw new Error(`${scriptPath} does not exist`)
}
const babel = require.resolve('./babel')
const loader = require.resolve('./pluginLoader')
const requireContext = require.resolve('./requireContext')
const nodeArgs = ['-r', babel, '-r', loader, '-r', requireContext]
.concat(withToken ? ['-r', require.resolve('./configClient')] : [])
.concat(scriptPath)
.concat(args.extraArguments || [])
const proc = spawn(process.argv[0], nodeArgs, {stdio: 'inherit'})
proc.on('close', process.exit)
}