Skip to content

Commit

Permalink
feat: make scripts spawn with relative paths
Browse files Browse the repository at this point in the history
This way the full script is shorter when displayed
  • Loading branch information
Kent C. Dodds committed Sep 23, 2017
1 parent facfcf6 commit f9a4baf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
8 changes: 5 additions & 3 deletions src/__tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ test('appDirectory is the dirname to the package.json', () => {

test('resolveKcdScripts resolves to src/index.js when in the kcd-scripts package', () => {
mockPkg({pkg: {name: 'kcd-scripts'}})
expect(require('../utils').resolveKcdScripts()).toBe(require.resolve('../'))
expect(require('../utils').resolveKcdScripts()).toBe(
require.resolve('../').replace(process.cwd(), '.'),
)
})

test('resolveKcdScripts resolves to kcd-scripts if not in the kcd-scripts package', () => {
Expand All @@ -36,13 +38,13 @@ test('resolveKcdScripts resolves to kcd-scripts if not in the kcd-scripts packag

test(`resolveBin resolves to the full path when it's not in $PATH`, () => {
expect(require('../utils').resolveBin('cross-env')).toBe(
require.resolve('cross-env/dist/bin/cross-env'),
require.resolve('cross-env/dist/bin/cross-env').replace(process.cwd(), '.'),
)
})

test(`resolveBin resolves to the binary if it's in $PATH`, () => {
whichSyncMock.mockImplementationOnce(() =>
require.resolve('cross-env/dist/bin/cross-env'),
require.resolve('cross-env/dist/bin/cross-env').replace(process.cwd(), '.'),
)
expect(require('../utils').resolveBin('cross-env')).toBe('cross-env')
expect(whichSyncMock).toHaveBeenCalledTimes(1)
Expand Down
11 changes: 2 additions & 9 deletions src/scripts/build/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,8 @@ if (!useSpecifiedOutDir && !args.includes('--no-clean')) {

const result = spawn.sync(
resolveBin('babel-cli', {executable: 'babel'}),
// prettier-ignore
[
...outDir,
...copyFiles,
...ignore,
...config,
'src',
].concat(args),
{stdio: 'inherit'},
[...outDir, ...copyFiles, ...ignore, ...config, 'src'].concat(args),
{stdio: 'inherit'}
)

process.exit(result.status)
3 changes: 2 additions & 1 deletion src/scripts/build/rollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ const crossEnv = resolveBin('cross-env')
const rollup = resolveBin('rollup')
const args = process.argv.slice(2)
const here = p => path.join(__dirname, p)
const hereRelative = p => here(p).replace(process.cwd(), '.')
const parsedArgs = yargsParser(args)

const useBuiltinConfig =
!args.includes('--config') && !hasFile('rollup.config.js')
const config = useBuiltinConfig
? `--config ${here('../../config/rollup.config.js')}`
? `--config ${hereRelative('../../config/rollup.config.js')}`
: args.includes('--config') ? '' : '--config' // --config will pick up the rollup.config.js file

const environment = parsedArgs.environment
Expand Down
5 changes: 3 additions & 2 deletions src/scripts/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ const args = process.argv.slice(2)
const parsedArgs = yargsParser(args)

const here = p => path.join(__dirname, p)
const hereRelative = p => here(p).replace(process.cwd(), '.')

const useBuiltinConfig =
!args.includes('--config') &&
!hasFile('.prettierrc') &&
!hasPkgProp('prettierrc')
const config = useBuiltinConfig
? ['--config', here('../config/prettierrc.js')]
? ['--config', hereRelative('../config/prettierrc.js')]
: []

const useBuiltinIgnore =
!args.includes('--ignore-path') && !hasFile('.prettierignore')
const ignore = useBuiltinIgnore
? ['--ignore-path', here('../config/prettierignore')]
? ['--ignore-path', hereRelative('../config/prettierignore')]
: []

const write = args.includes('--no-write') ? [] : ['--write']
Expand Down
5 changes: 3 additions & 2 deletions src/scripts/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ const {hasPkgProp, resolveBin, hasFile} = require('../utils')

let args = process.argv.slice(2)
const here = p => path.join(__dirname, p)
const hereRelative = p => here(p).replace(process.cwd(), '.')
const parsedArgs = yargsParser(args)

const useBuiltinConfig =
!args.includes('--config') &&
!hasFile('.eslintrc') &&
!hasPkgProp('eslintConfig')
const config = useBuiltinConfig
? ['--config', here('../config/eslintrc.js')]
? ['--config', hereRelative('../config/eslintrc.js')]
: []

const useBuiltinIgnore =
Expand All @@ -21,7 +22,7 @@ const useBuiltinIgnore =
!hasPkgProp('eslintIgnore')

const ignore = useBuiltinIgnore
? ['--ignore-path', here('../config/eslintignore')]
? ['--ignore-path', hereRelative('../config/eslintignore')]
: []

const cache = args.includes('--no-cache') ? [] : ['--cache']
Expand Down
6 changes: 3 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const appDirectory = path.dirname(pkgPath)

function resolveKcdScripts() {
if (pkg.name === 'kcd-scripts') {
return require.resolve('./')
return require.resolve('./').replace(process.cwd(), '.')
}
return resolveBin('kcd-scripts')
}

// eslint-disable-next-line complexity
function resolveBin(modName, {executable = modName} = {}) {
function resolveBin(modName, {executable = modName, cwd = process.cwd()} = {}) {
let pathFromWhich
try {
pathFromWhich = fs.realpathSync(which.sync(executable))
Expand All @@ -34,7 +34,7 @@ function resolveBin(modName, {executable = modName} = {}) {
if (fullPathToBin === pathFromWhich) {
return executable
}
return fullPathToBin
return fullPathToBin.replace(cwd, '.')
} catch (error) {
if (pathFromWhich) {
return executable
Expand Down

0 comments on commit f9a4baf

Please sign in to comment.