Skip to content

Commit

Permalink
feat(cli): add symlink type customization
Browse files Browse the repository at this point in the history
closes #13
  • Loading branch information
antongolub committed Aug 10, 2020
1 parent cd2efab commit bfb2747
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ success Already up-to-date.
|`--only` | Set package [updating scope](https://docs.npmjs.com/cli/audit): `dev`/`prod`
|`--force` | Have audit fix install semver-major updates to toplevel dependencies, not just semver-compatible ones | false
|`--audit-level` | Include a vulnerability with a level as defined or higher | moderate
|`--symlink` | Specify symlink type for temp assets: `dir` or `junction` (Windows only) | dir

## License
[MIT](./LICENSE)
8 changes: 4 additions & 4 deletions src/main/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from 'fs-extra'
import fs, {SymlinkType} from 'fs-extra'
import synp from 'synp'
import {join} from 'path'
import findCacheDir from 'find-cache-dir'
import chalk from 'chalk'
import {invoke, formatFlags} from './util'
import {invoke, formatFlags, getSymlinkType} from './util'

type TContext = { cwd: string, temp: string, flags: Record<string, any> }

Expand All @@ -16,10 +16,10 @@ type TStage = [string, ...TCallback[]]
* @param {TContext} cxt
* @return {void}
*/
const createTempAssets: TCallback = ({temp}) => {
const createTempAssets: TCallback = ({temp, flags}) => {
fs.copyFileSync('yarn.lock', join(temp, 'yarn.lock'))
fs.copyFileSync('package.json', join(temp, 'package.json'))
fs.createSymlinkSync('node_modules', join(temp, 'node_modules'), 'dir')
fs.createSymlinkSync('node_modules', join(temp, 'node_modules'), getSymlinkType(flags.symlink) as SymlinkType) // TODO fix fs-extra typings issue
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/main/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cp from 'child_process'
import chalk from 'chalk'
import {FsSymlinkType} from 'fs-extra'

export const invoke = (cmd: string, args: string[], cwd: string) => {
console.log(chalk.bold('invoke'), cmd, ...args)
Expand All @@ -26,3 +27,10 @@ export const formatFlags = (flags: Record<string, any>, ...picklist: string[]):

return memo
}, [])

export const isWindows = () => process.platform === 'win32' || /^(msys|cygwin)$/.test(process.env.OSTYPE as string)

export const getSymlinkType = (type?: string): FsSymlinkType =>
type === 'junction' && isWindows()
? type
: 'dir'
16 changes: 15 additions & 1 deletion src/test/ts/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {formatFlags} from '../../main/ts/util'
import {formatFlags, getSymlinkType} from '../../main/ts/util'
import minimist from 'minimist'

describe('util', () => {
Expand Down Expand Up @@ -28,4 +28,18 @@ describe('util', () => {
})
})
})

describe('#getSymlinkType', () => {
it('resolves type by system profile and arg', () => {
process.env.OSTYPE = 'msys'
expect(getSymlinkType('junction')).toBe('junction')
expect(getSymlinkType('foo')).toBe('dir')
expect(getSymlinkType()).toBe('dir')

process.env.OSTYPE = 'unknown'
expect(getSymlinkType('junction')).toBe('dir')
expect(getSymlinkType('foo')).toBe('dir')
expect(getSymlinkType()).toBe('dir')
})
})
})

0 comments on commit bfb2747

Please sign in to comment.