Skip to content

Commit 8aa0915

Browse files
rexxarsbjoerge
authored andcommitted
[core] Add sanity exec command that registers part loader + babel (#138)
1 parent d993c75 commit 8aa0915

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

packages/@sanity/core/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616
"dependencies": {
1717
"@sanity/check": "^0.108.0",
1818
"@sanity/mutator": "^0.108.0",
19+
"@sanity/plugin-loader": "^0.108.0",
1920
"@sanity/resolver": "^0.108.0",
2021
"@sanity/server": "^0.108.0",
2122
"@sanity/util": "^0.108.0",
23+
"babel-preset-es2015-node4": "^2.1.1",
24+
"babel-preset-stage-2": "^6.22.0",
25+
"babel-register": "^6.26.0",
2226
"batch-stream-operation": "^1.0.2",
2327
"debug": "^2.6.3",
2428
"deep-sort-object": "^1.0.1",
@@ -43,8 +47,6 @@
4347
},
4448
"devDependencies": {
4549
"babel-plugin-lodash": "^3.2.11",
46-
"babel-preset-es2015-node4": "^2.1.1",
47-
"babel-preset-stage-2": "^6.22.0",
4850
"chai": "^3.5.0",
4951
"chai-as-promised": "^6.0.0",
5052
"eslint": "^3.19.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const presets = ['es2015-node4', 'stage-2']
2+
3+
require('babel-register')({
4+
presets: presets.map(preset => require.resolve(`babel-preset-${preset}`)),
5+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const spawn = require('child_process').spawn
2+
const fsp = require('fs-promise')
3+
const path = require('path')
4+
5+
module.exports = async args => {
6+
const [script] = args.argsWithoutOptions
7+
const scriptPath = path.resolve(script)
8+
9+
if (!script) {
10+
throw new Error('SCRIPT must be provided. `sanity exec <script>`')
11+
}
12+
13+
if (!await fsp.exists(scriptPath)) {
14+
throw new Error(`${scriptPath} does not exist`)
15+
}
16+
17+
const babel = require.resolve('./babel')
18+
const loader = require.resolve('@sanity/plugin-loader/register')
19+
const proc = spawn(process.argv[0], ['-r', babel, '-r', loader, scriptPath])
20+
21+
proc.stdout.pipe(process.stdout)
22+
proc.stderr.pipe(process.stderr)
23+
proc.on('close', process.exit)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import lazyRequire from '@sanity/util/lib/lazyRequire'
2+
3+
export default {
4+
name: 'exec',
5+
signature: 'SCRIPT',
6+
description: 'Runs a script in Sanity context',
7+
action: lazyRequire(require.resolve('../../actions/exec/execScript'))
8+
}

packages/@sanity/core/src/commands/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import deleteHookCommand from './hook/deleteHookCommand'
2020
import listHooksCommand from './hook/listHooksCommand'
2121
import printHookAttemptCommand from './hook/printHookAttemptCommand'
2222
import listHookLogsCommand from './hook/listHookLogsCommand'
23+
import execCommand from './exec/execCommand'
2324

2425
export default [
2526
buildCommand,
@@ -43,5 +44,6 @@ export default [
4344
queryDocumentsCommand,
4445
installCommand,
4546
startCommand,
46-
uninstallCommand
47+
uninstallCommand,
48+
execCommand
4749
]

0 commit comments

Comments
 (0)