Skip to content

Commit

Permalink
[core] Add sanity exec command that registers part loader + babel (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars authored and bjoerge committed Aug 24, 2017
1 parent d993c75 commit 8aa0915
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
6 changes: 4 additions & 2 deletions packages/@sanity/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
"dependencies": {
"@sanity/check": "^0.108.0",
"@sanity/mutator": "^0.108.0",
"@sanity/plugin-loader": "^0.108.0",
"@sanity/resolver": "^0.108.0",
"@sanity/server": "^0.108.0",
"@sanity/util": "^0.108.0",
"babel-preset-es2015-node4": "^2.1.1",
"babel-preset-stage-2": "^6.22.0",
"babel-register": "^6.26.0",
"batch-stream-operation": "^1.0.2",
"debug": "^2.6.3",
"deep-sort-object": "^1.0.1",
Expand All @@ -43,8 +47,6 @@
},
"devDependencies": {
"babel-plugin-lodash": "^3.2.11",
"babel-preset-es2015-node4": "^2.1.1",
"babel-preset-stage-2": "^6.22.0",
"chai": "^3.5.0",
"chai-as-promised": "^6.0.0",
"eslint": "^3.19.0",
Expand Down
5 changes: 5 additions & 0 deletions packages/@sanity/core/src/actions/exec/babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const presets = ['es2015-node4', 'stage-2']

require('babel-register')({
presets: presets.map(preset => require.resolve(`babel-preset-${preset}`)),
})
24 changes: 24 additions & 0 deletions packages/@sanity/core/src/actions/exec/execScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const spawn = require('child_process').spawn
const fsp = require('fs-promise')
const path = require('path')

module.exports = async args => {
const [script] = args.argsWithoutOptions
const scriptPath = path.resolve(script)

if (!script) {
throw new Error('SCRIPT must be provided. `sanity exec <script>`')
}

if (!await fsp.exists(scriptPath)) {
throw new Error(`${scriptPath} does not exist`)
}

const babel = require.resolve('./babel')
const loader = require.resolve('@sanity/plugin-loader/register')
const proc = spawn(process.argv[0], ['-r', babel, '-r', loader, scriptPath])

proc.stdout.pipe(process.stdout)
proc.stderr.pipe(process.stderr)
proc.on('close', process.exit)
}
8 changes: 8 additions & 0 deletions packages/@sanity/core/src/commands/exec/execCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import lazyRequire from '@sanity/util/lib/lazyRequire'

export default {
name: 'exec',
signature: 'SCRIPT',
description: 'Runs a script in Sanity context',
action: lazyRequire(require.resolve('../../actions/exec/execScript'))
}
4 changes: 3 additions & 1 deletion packages/@sanity/core/src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import deleteHookCommand from './hook/deleteHookCommand'
import listHooksCommand from './hook/listHooksCommand'
import printHookAttemptCommand from './hook/printHookAttemptCommand'
import listHookLogsCommand from './hook/listHookLogsCommand'
import execCommand from './exec/execCommand'

export default [
buildCommand,
Expand All @@ -43,5 +44,6 @@ export default [
queryDocumentsCommand,
installCommand,
startCommand,
uninstallCommand
uninstallCommand,
execCommand
]

0 comments on commit 8aa0915

Please sign in to comment.