Skip to content

Commit

Permalink
:rage3: windows working and fight node modules (#179)
Browse files Browse the repository at this point in the history
* windows working

* improve style and verbiage
  • Loading branch information
GantMan authored Feb 27, 2018
1 parent e0c7652 commit 3b0cede
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .vscode/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"config",
"configs",
"custom",
"delineator",
"docsify",
"e.g.",
"env",
Expand Down Expand Up @@ -46,6 +47,7 @@
"specs",
"tada",
"tempy",
"typesync",
"updtr",
"v1",
"walkthrough"
Expand Down
4 changes: 4 additions & 0 deletions __tests__/__mocks__/mockContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ const noConfigSolidarity = {
setSolidaritySettings: jest.fn(),
updateRequirement: jest.fn(),
updateVersions: jest.fn(() => Promise.resolve()),
getLineWithVersion: jest.fn(),
removeNonVersionCharacters: jest.fn(),
}

const mockContext = {
...realThing,
outputMode: undefined,
system: {
startTimer: jest.fn(() => jest.fn()),
run: jest.fn(() => '12'),
which: jest.fn(name => 'usr/local/bin/${name}'),
},
template: {
generate: jest.fn(),
Expand Down
9 changes: 9 additions & 0 deletions __tests__/command_helpers/checkCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const alwaysExistCLI = {
binary: 'node',
}

const badSemver = {
binary: 'node',
semver: 'wtfbbq!!!11',
}

const outOfDateCLI = {
binary: 'node',
semver: '10.99',
Expand All @@ -24,6 +29,10 @@ test('fine on existing binary', async () => {
expect(await checkCLI(alwaysExistCLI, context)).toBe(undefined)
})

test('errors with message when an improper semver is sent', async () => {
expect(await checkCLI(badSemver, context)).toBe(`Invalid semver rule ${badSemver.semver}`)
})

test('returns message on improper version', async () => {
solidarityExtension(context)
context.solidarity.getVersion = () => '1'
Expand Down
2 changes: 1 addition & 1 deletion __tests__/command_helpers/getVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('getVersion', () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000
})

afterAll(function () {
afterAll(function() {
// Fix timeout change
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout
})
Expand Down
32 changes: 32 additions & 0 deletions __tests__/command_helpers/quirksNodeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const path = require('path')
const delineator = process.platform === 'win32' ? ';' : ':'
test('quirks moves node_modules to back', () => {
// key is that it has `node_modules` + path.sep
const injectedStuff = `node_modules${path.sep}testOnly`
// prepend to PATH
process.env.PATH = injectedStuff + delineator + process.env.PATH
const pathAsArray = process.env.PATH.split(delineator)
// Yup it is prepended to front
expect(pathAsArray[0]).toBe(injectedStuff)
// require mutates PATH
require('../../src/extensions/functions/quirksNodeModules')
const newPathAsArray = process.env.PATH.split(delineator)
// Not in the front
expect(newPathAsArray[0]).not.toBe(injectedStuff)
// still there though (moved to back)
expect(process.env.PATH.includes(injectedStuff)).toBeTruthy()
})

test('quirks does not move just any injected path to back', () => {
const injectedStuff = `taco${path.sep}testOnly`
// prepend to PATH
process.env.PATH = injectedStuff + delineator + process.env.PATH
const pathAsArray = process.env.PATH.split(delineator)
// Yup it is prepended to front
expect(pathAsArray[0]).toBe(injectedStuff)
// require does not mutate PATH this time
require('../../src/extensions/functions/quirksNodeModules')
const newPathAsArray = process.env.PATH.split(delineator)
// Not in the front
expect(newPathAsArray[0]).toBe(injectedStuff)
})
2 changes: 2 additions & 0 deletions src/extensions/functions/checkCLI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { SolidarityRunContext, CLIRule } from '../../types'
module.exports = async (rule: CLIRule, context: SolidarityRunContext): Promise<string | undefined> => {
const { semver, solidarity } = context
const binaryExists = require('./binaryExists')
// Node Modules do strange things
require('./quirksNodeModules')

// First check for binary
if (!binaryExists(rule.binary, context)) {
Expand Down
3 changes: 2 additions & 1 deletion src/extensions/functions/checkCLIForUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = async (rule: CLIRule, context: SolidarityRunContext): Promise<s
// if it doesn't satisfy, upgrade
if (rule.semver && !semver.satisfies(binarySemantic, rule.semver)) {
rule.semver = binaryVersion
return print.colors.green(`Setting ${rule.binary} to '${binaryVersion}'`)
const lineMessage = rule.line ? ` line ${rule.line}` : ''
return print.colors.green(`Setting ${rule.binary}${lineMessage} to '${binaryVersion}'`)
}
}
9 changes: 9 additions & 0 deletions src/extensions/functions/quirksNodeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { reject, contains, concat, difference } from 'ramda'
const path = require('path')
const delineator = process.platform === 'win32' ? ';' : ':'

// Node mutates path by adding to the front, move that to the back if it exists
const originalPath = process.env.PATH || ''
const originalArray = originalPath.split(delineator)
const cleanArray = reject(contains('node_modules' + path.sep), originalArray)
process.env.PATH = concat(cleanArray, difference(originalArray, cleanArray)).join(delineator)
3 changes: 2 additions & 1 deletion src/extensions/functions/updateRequirement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ module.exports = async (
case 'cli':
if (!rule.semver) return []
const updateResult = await checkCLIForUpdates(rule, context)
ruleString = `Keep ${rule.binary} ${rule.semver}`
const lineMessage = rule.line ? ` line ${rule.line} at` : ''
ruleString = `Keep ${rule.binary}${lineMessage} ${rule.semver}`
if (updateResult) {
spinner.succeed(updateResult)
return updateResult
Expand Down

0 comments on commit 3b0cede

Please sign in to comment.