From 2d706fa8b7ba554fd102949afee2b688ce364e2e Mon Sep 17 00:00:00 2001 From: Gant Date: Sat, 4 Aug 2018 14:16:34 -0500 Subject: [PATCH 01/19] starting check option --- .npmignore | 1 + __tests__/commands/create.ts | 2 +- __tests__/commands/help.ts | 2 +- src/commands/create.ts | 2 +- src/commands/help.ts | 1 + src/commands/solidarity.ts | 60 +++++++++++++++++++++++++++++------- 6 files changed, 54 insertions(+), 14 deletions(-) diff --git a/.npmignore b/.npmignore index d392179..e4ee23d 100644 --- a/.npmignore +++ b/.npmignore @@ -6,3 +6,4 @@ src/ coverage/ yarn.lock *.log +check/ diff --git a/__tests__/commands/create.ts b/__tests__/commands/create.ts index 087e252..af7ae66 100644 --- a/__tests__/commands/create.ts +++ b/__tests__/commands/create.ts @@ -13,7 +13,7 @@ it('enforces required properties', () => { test('check solidarity create with no parameter', async () => { await createCommand.run(mockContext) - expect(mockContext.print.error.mock.calls).toEqual([['Missing what to create'], ['solidarity create ']]) + expect(mockContext.print.error.mock.calls).toEqual([['Missing what to create'], ['$ solidarity create ']]) expect(mockContext.print.info.mock.calls.length).toBe(1) }) diff --git a/__tests__/commands/help.ts b/__tests__/commands/help.ts index ab901bf..712880e 100644 --- a/__tests__/commands/help.ts +++ b/__tests__/commands/help.ts @@ -27,7 +27,7 @@ test('Calls print items several times', () => { expect(context.print.success.mock.calls.length).toBe(0) expect(context.print.colors.magenta.mock.calls.length).toBe(0) helpCommand.run(context) - expect(context.print.info.mock.calls.length).toBe(8) + expect(context.print.info.mock.calls.length).toBe(9) expect(context.print.printCommands.mock.calls.length).toBe(1) expect(context.print.success.mock.calls.length).toBe(2) expect(context.print.colors.magenta.mock.calls.length).toBe(2) diff --git a/src/commands/create.ts b/src/commands/create.ts index 0a540fe..0fc8e30 100644 --- a/src/commands/create.ts +++ b/src/commands/create.ts @@ -18,7 +18,7 @@ module.exports = { break default: print.error('Missing what to create') - print.error('solidarity create ') + print.error('$ solidarity create ') print.info(`Things you can create: ${createables}`) } }, diff --git a/src/commands/help.ts b/src/commands/help.ts index 6ec85a8..ac44e94 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -15,6 +15,7 @@ module.exports = { print.info(' --silent\t (-s) No output, just a return code of success/failure') print.info(' --solidarityFile\t (-f) Use given path to solidarity file for settings') print.info(' --module\t (-m) Search for a solidarity file in the given npm package') + print.info(' --check\t (-c) Use a known technology, and not the local file') print.success(colors.magenta('\nSolidarity is open source - https://github.com/infinitered/solidarity')) print.info(colors.magenta('If you need additional help, join our Slack at http://community.infinite.red')) diff --git a/src/commands/solidarity.ts b/src/commands/solidarity.ts index 992eeba..a4beb54 100644 --- a/src/commands/solidarity.ts +++ b/src/commands/solidarity.ts @@ -41,21 +41,59 @@ namespace Solidarity { // drop out fast in these situations await checkForEscapeHatchFlags(context) - const { print, solidarity } = context + const { print, solidarity, parameters } = context + const { options } = parameters const { checkRequirement, getSolidaritySettings } = solidarity // get settings or error let solidaritySettings - try { - solidaritySettings = getSolidaritySettings(context) - } catch (e) { - print.error(e) - print.info( - `Make sure you are in the correct folder or run ${print.colors.success( - 'solidarity snapshot' - )} to take a snapshot of your environment and create a .solidarity file for this project.` - ) - process.exit(3) + ////////// + // Consider cleaing this up + const checkOption: string = options ? (options.check || options.c) : null + if (checkOption) { + const { http } = context + const checkSpinner = print.spin(`Running check on ${checkOption}`) + const api = http.create({ + baseURL: 'https://api.github.com', + headers: { Accept: 'application/vnd.github.v3+json' }, + }) + try { + // Load check from web + const checkURL = `https:\/\/raw.githubusercontent.com/infinitered/solidarity/master/${checkOption}` + const result = await api.get(checkURL) + console.log(result) + if (result.ok) { + checkSpinner.succeed(checkURL) + // result.data + } else { + checkSpinner.fail(`Unable to find a known check for ${checkOption}`) + print.info( + `https://github.com/infinitered/solidarity/checks for options.` + ) + process.exit(3) + } + } catch (e) { + checkSpinner.fail(e) + print.info( + `Check is meant to verify your environment based on known stacks + solidarity failed to do a proper check for ${print.colors.success(checkOption)}` + ) + process.exit(3) + } + process.exit(1) + ////////////////////////////////////// + } else { + try { + solidaritySettings = getSolidaritySettings(context) + } catch (e) { + print.error(e) + print.info( + `Make sure you are in the correct folder or run ${print.colors.success( + 'solidarity snapshot' + )} to take a snapshot of your environment and create a .solidarity file for this project.` + ) + process.exit(3) + } } context.outputMode = setOutputMode(context.parameters, solidaritySettings) From 97b446084011a773f391dcc303b6a90ff962630e Mon Sep 17 00:00:00 2001 From: Gant Date: Sat, 4 Aug 2018 14:56:19 -0500 Subject: [PATCH 02/19] primed to move functionality down to function --- src/commands/solidarity.ts | 2 +- .../functions/getSolidaritySettings.ts | 64 ++++++++++++------- 2 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/commands/solidarity.ts b/src/commands/solidarity.ts index a4beb54..06d68af 100644 --- a/src/commands/solidarity.ts +++ b/src/commands/solidarity.ts @@ -48,7 +48,7 @@ namespace Solidarity { // get settings or error let solidaritySettings ////////// - // Consider cleaing this up + // Consider cleaning this up const checkOption: string = options ? (options.check || options.c) : null if (checkOption) { const { http } = context diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index 606cc76..e631970 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -1,38 +1,58 @@ -import { SolidarityRunContext, SolidaritySettings } from '../../types' +import { SolidarityRunContext, SolidaritySettings, solidarity } from '../../types' import * as JSON5 from 'json5' import * as path from 'path' +export const loadFile = (context, filePath) => { + const { filesystem } = context + if (filesystem.exists(filePath)) { + return JSON5.parse(filesystem.read('.solidarity')) + } else { + throw 'ERROR: There is no solidarity file at the given path' + } +} + +export const loadModule = (context, moduleName) => { + const { filesystem } = context + // We will search that module + const filePath = path.join('node_modules', moduleName, '.solidarity') + + if (filesystem.exists(filePath)) { + return JSON5.parse(filesystem.read(filePath)) + } else if (filesystem.exists(filePath + '.json')) { + return JSON5.parse(filesystem.read(filePath + '.json')) + } else { + throw 'ERROR: There is no solidarity file found with the given module' + } +} + +export const loadWebCheck = (context, checkStack) => { + +} + +export const loadDefault = () => { + +} + module.exports = (context: SolidarityRunContext): SolidaritySettings => { const { filesystem, parameters } = context const options = parameters.options || {} // fix possibly undefined from gluegun + const demandedFile = options.solidarityFile || options.f + const demandedModule = options.module || options.m + const demandedCheck = options.check || options.c /* for now only JSON and JSON5 support * Summary: * Looks for `.solidarity` or `.solidarity.json` files * Unless you pass parameter options telling us to look - * in specific paths or node modules + * in specific paths, node modules, or websites */ let solidaritySettings - if (options.solidarityFile || options.f) { - // They are telling us where to go - const filePath = options.solidarityFile || options.f - if (filesystem.exists(filePath)) { - solidaritySettings = JSON5.parse(filesystem.read('.solidarity')) - } else { - throw 'ERROR: There is no solidarity file at the given path' - } - } else if (options.module || options.m) { - // We will search that module - const moduleName = options.module || options.m - const filePath = path.join('node_modules', moduleName, '.solidarity') - - if (filesystem.exists(filePath)) { - solidaritySettings = JSON5.parse(filesystem.read(filePath)) - } else if (filesystem.exists(filePath + '.json')) { - solidaritySettings = JSON5.parse(filesystem.read(filePath + '.json')) - } else { - throw 'ERROR: There is no solidarity file found with the given module' - } + if (demandedFile) { + solidaritySettings = loadFile(context, demandedFile) + } else if (demandedModule) { + solidaritySettings = loadModule(context, demandedModule) + } else if (demandedCheck) { + solidaritySettings = loadWebCheck(context, demandedCheck) } else if (filesystem.exists('.solidarity')) { solidaritySettings = JSON5.parse(filesystem.read('.solidarity')) } else if (filesystem.exists('.solidarity.json')) { From bdaf8e430b30f31d927ead67976cf3b4f90f3f67 Mon Sep 17 00:00:00 2001 From: Gant Date: Sat, 4 Aug 2018 16:44:47 -0500 Subject: [PATCH 03/19] shifting getSettings to async + tests --- __tests__/__mocks__/mockContext.ts | 2 +- .../appendSolidaritySettings.ts | 14 ++-- .../command_helpers/getSolidaritySettings.ts | 84 ++++++++++--------- src/commands/report.ts | 2 +- src/commands/snapshot.ts | 4 +- src/commands/solidarity.ts | 60 +++---------- .../functions/appendSolidaritySettings.ts | 4 +- .../functions/buildSpecificRequirement.ts | 2 +- .../functions/getSolidaritySettings.ts | 32 +++++-- src/extensions/functions/updateVersions.ts | 2 +- 10 files changed, 93 insertions(+), 113 deletions(-) diff --git a/__tests__/__mocks__/mockContext.ts b/__tests__/__mocks__/mockContext.ts index d7478e8..c0f1b6b 100644 --- a/__tests__/__mocks__/mockContext.ts +++ b/__tests__/__mocks__/mockContext.ts @@ -4,7 +4,7 @@ realSolidarityContext(realThing) const noConfigSolidarity = { checkRequirement: jest.fn(), - getSolidaritySettings: jest.fn(() => ({})), + getSolidaritySettings: jest.fn(() => Promise.resolve({})), printResults: jest.fn(), setSolidaritySettings: jest.fn(), updateRequirement: jest.fn(), diff --git a/__tests__/command_helpers/appendSolidaritySettings.ts b/__tests__/command_helpers/appendSolidaritySettings.ts index 548d6e8..16e23eb 100644 --- a/__tests__/command_helpers/appendSolidaritySettings.ts +++ b/__tests__/command_helpers/appendSolidaritySettings.ts @@ -18,11 +18,11 @@ describe('appendSolidaritySettings', () => { context.solidarity = { ...context.solidarity, - getSolidaritySettings: jest.fn(() => solidaritySettings), + getSolidaritySettings: jest.fn(() => Promise.resolve(solidaritySettings)), } }) - it('appends the given requirement to the existing settings', () => { + it('appends the given requirement to the existing settings', async () => { const newRequirement = { three: [{ rule: 'cli' }], } @@ -31,13 +31,13 @@ describe('appendSolidaritySettings', () => { first: 'cli', } - const newSettings = appendSolidaritySettings(context, newRequirement) + const newSettings = await appendSolidaritySettings(context, newRequirement) expect(keys(newSettings.requirements).length).toEqual(3) expect(keys(newSettings.requirements.three).length).toEqual(1) }) - it('will append the given requirement to and existing requirement', () => { + it('will append the given requirement to and existing requirement', async () => { context.parameters = { first: 'cli', second: 'ruby', @@ -47,7 +47,7 @@ describe('appendSolidaritySettings', () => { twoTest: [{ rule: 'cli', binary: 'ruby' }], } - let newSettings = appendSolidaritySettings(context, newRequirement) + let newSettings = await appendSolidaritySettings(context, newRequirement) expect(keys(newSettings.requirements).length).toEqual(2) expect(newSettings.requirements.twoTest.length).toEqual(2) @@ -57,7 +57,7 @@ describe('appendSolidaritySettings', () => { }) describe('given a requirement with a prexisting rule', () => { - it('should just merge the rule w/ the existing rule', () => { + it('should just merge the rule w/ the existing rule', async () => { context.parameters = { first: 'env', } @@ -73,7 +73,7 @@ describe('appendSolidaritySettings', () => { ], } - let newSettings = appendSolidaritySettings(context, newRequirement) + let newSettings = await appendSolidaritySettings(context, newRequirement) expect(keys(newSettings.requirements).length).toEqual(2) expect(newSettings.requirements.oneTest.length).toEqual(2) diff --git a/__tests__/command_helpers/getSolidaritySettings.ts b/__tests__/command_helpers/getSolidaritySettings.ts index 99b4aca..27105ad 100644 --- a/__tests__/command_helpers/getSolidaritySettings.ts +++ b/__tests__/command_helpers/getSolidaritySettings.ts @@ -6,68 +6,70 @@ describe('basic getSolidaritySettings', () => { describe('w/ success', () => { test('getSolidaritySettings exists', () => expect(getSolidaritySettings).toMatchSnapshot()) - test('getSolidaritySettings succeeds', () => { - const resultSettings = getSolidaritySettings(context) + test('getSolidaritySettings succeeds', async () => { + const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) }) - test('getSolidaritySettings succeeds', () => { + test('getSolidaritySettings succeeds', async () => { process.chdir('__tests__/sandbox/solidarity_json') - const resultSettings = getSolidaritySettings(context) + const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) process.chdir('../../../') }) }) - describe('w/ failure', () => { - test('getSolidaritySettings can fail', () => { - expect(() => { - process.chdir('__tests__') - getSolidaritySettings(context) - }).toThrow() - process.chdir('../') - }) + // describe('w/ failure', () => { + // test('getSolidaritySettings can fail', async () => { + // await expect(async () => { + // process.chdir('__tests__') + // await getSolidaritySettings(context) + // }).toThrow() + // process.chdir('../') + // }) - test('getSolidaritySettings can warn with missing requirements', () => { - expect(() => { - process.chdir('__tests__/sandbox/solidarity_broken') - getSolidaritySettings(context) - }).toThrowError('ERROR: Found, but no requirements key. Please validate your solidarity file') - process.chdir('../../../') - }) - }) + // test('getSolidaritySettings can warn with missing requirements', async () => { + // await expect(async () => { + // process.chdir('__tests__/sandbox/solidarity_broken') + // await getSolidaritySettings(context) + // }).toThrowError('ERROR: Found, but no requirements key. Please validate your solidarity file') + // process.chdir('../../../') + // }) + // }) }) describe('parameterized getSolidaritySettings', () => { - test('custom path with -f', () => { + test('custom path with -f', async () => { context.parameters.options = { f: '__tests__/sandbox/solidarity_json' } - const resultSettings = getSolidaritySettings(context) + const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) context.parameters.options = {} }) - test('custom path with --solidarityFile', () => { + test('custom path with --solidarityFile', async () => { context.parameters.options = { solidarityFile: '__tests__/sandbox/solidarity_json' } - const resultSettings = getSolidaritySettings(context) + const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) context.parameters.options = {} }) - test('failing path message', () => { + test('failing path message', async () => { // test longhand context.parameters.options = { solidarityFile: '__tests__/fake' } - expect(() => { - getSolidaritySettings(context) - }).toThrowError('ERROR: There is no solidarity file at the given path') + await expect(getSolidaritySettings(context)) + .rejects + .toThrow('ERROR: There is no solidarity file at the given path') + // test shorthand context.parameters.options = { f: '__tests__/fake' } - expect(() => { - getSolidaritySettings(context) - }).toThrowError('ERROR: There is no solidarity file at the given path') + await expect(getSolidaritySettings(context)) + .rejects + .toThrow('ERROR: There is no solidarity file at the given path') + context.parameters.options = {} }) @@ -76,35 +78,35 @@ describe('parameterized getSolidaritySettings', () => { process.chdir('__tests__/sandbox/fake_project') }) - test('can find solidarity file in module with flag -m', () => { + test('can find solidarity file in module with flag -m', async () => { context.parameters.options = { m: 'mock_module' } - const resultSettings = getSolidaritySettings(context) + const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) context.parameters.options = {} }) - test('can find solidarity file in module with flag --module', () => { + test('can find solidarity file in module with flag --module', async () => { context.parameters.options = { module: 'mock_module' } - const resultSettings = getSolidaritySettings(context) + const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) context.parameters.options = {} }) - test('can find solidarity JSON file in module with flag --module', () => { + test('can find solidarity JSON file in module with flag --module', async () => { context.parameters.options = { module: 'mock_second_module' } - const resultSettings = getSolidaritySettings(context) + const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) context.parameters.options = {} }) - test('errors if no solidarity file in module', () => { + test('errors if no solidarity file in module', async () => { context.parameters.options = { module: 'nope' } - expect(() => { - getSolidaritySettings(context) - }).toThrowError('ERROR: There is no solidarity file found with the given module') + await expect(getSolidaritySettings(context)) + .rejects + .toThrow('ERROR: There is no solidarity file found with the given module'); context.parameters.options = {} }) diff --git a/src/commands/report.ts b/src/commands/report.ts index 2d1abcd..4e31049 100644 --- a/src/commands/report.ts +++ b/src/commands/report.ts @@ -18,7 +18,7 @@ module.exports = { // get settings or error let solidaritySettings try { - solidaritySettings = getSolidaritySettings(context) + solidaritySettings = await getSolidaritySettings(context) } catch (e) { spinner.fail(`No valid ${print.colors.success('solidarity')} file was found to report.`) process.exit(3) diff --git a/src/commands/snapshot.ts b/src/commands/snapshot.ts index 4ec55ba..b88a9c6 100644 --- a/src/commands/snapshot.ts +++ b/src/commands/snapshot.ts @@ -75,8 +75,8 @@ namespace Snapshot { if (first) { await buildSpecificRequirement(context) - .then(newRequirement => { - const updatedSolidaritySettings = appendSolidaritySettings(context, newRequirement) + .then(async (newRequirement) => { + const updatedSolidaritySettings = await appendSolidaritySettings(context, newRequirement) setSolidaritySettings(updatedSolidaritySettings, context) }) diff --git a/src/commands/solidarity.ts b/src/commands/solidarity.ts index 06d68af..d0bd0e8 100644 --- a/src/commands/solidarity.ts +++ b/src/commands/solidarity.ts @@ -41,59 +41,21 @@ namespace Solidarity { // drop out fast in these situations await checkForEscapeHatchFlags(context) - const { print, solidarity, parameters } = context - const { options } = parameters + const { print, solidarity } = context const { checkRequirement, getSolidaritySettings } = solidarity // get settings or error let solidaritySettings - ////////// - // Consider cleaning this up - const checkOption: string = options ? (options.check || options.c) : null - if (checkOption) { - const { http } = context - const checkSpinner = print.spin(`Running check on ${checkOption}`) - const api = http.create({ - baseURL: 'https://api.github.com', - headers: { Accept: 'application/vnd.github.v3+json' }, - }) - try { - // Load check from web - const checkURL = `https:\/\/raw.githubusercontent.com/infinitered/solidarity/master/${checkOption}` - const result = await api.get(checkURL) - console.log(result) - if (result.ok) { - checkSpinner.succeed(checkURL) - // result.data - } else { - checkSpinner.fail(`Unable to find a known check for ${checkOption}`) - print.info( - `https://github.com/infinitered/solidarity/checks for options.` - ) - process.exit(3) - } - } catch (e) { - checkSpinner.fail(e) - print.info( - `Check is meant to verify your environment based on known stacks - solidarity failed to do a proper check for ${print.colors.success(checkOption)}` - ) - process.exit(3) - } - process.exit(1) - ////////////////////////////////////// - } else { - try { - solidaritySettings = getSolidaritySettings(context) - } catch (e) { - print.error(e) - print.info( - `Make sure you are in the correct folder or run ${print.colors.success( - 'solidarity snapshot' - )} to take a snapshot of your environment and create a .solidarity file for this project.` - ) - process.exit(3) - } + try { + solidaritySettings = await getSolidaritySettings(context) + } catch (e) { + print.error(e) + print.info( + `Make sure you are in the correct folder or run ${print.colors.success( + 'solidarity snapshot' + )} to take a snapshot of your environment and create a .solidarity file for this project.` + ) + process.exit(3) } context.outputMode = setOutputMode(context.parameters, solidaritySettings) diff --git a/src/extensions/functions/appendSolidaritySettings.ts b/src/extensions/functions/appendSolidaritySettings.ts index c978dd8..c7ef4ad 100644 --- a/src/extensions/functions/appendSolidaritySettings.ts +++ b/src/extensions/functions/appendSolidaritySettings.ts @@ -20,12 +20,12 @@ const updateExistingRule = (solidaritySettings, updatedRequirementRules, newRequ } } -module.exports = (context, newRequirement) => { +module.exports = async (context, newRequirement) => { const { solidarity, parameters } = context const { getSolidaritySettings, ruleHandlers } = solidarity const { first } = parameters - const solidaritySettings = getSolidaritySettings(context) + const solidaritySettings = await getSolidaritySettings(context) const newRequirementKey = keys(newRequirement)[0] const existingRequirementRules = solidaritySettings.requirements[newRequirementKey] || [] diff --git a/src/extensions/functions/buildSpecificRequirement.ts b/src/extensions/functions/buildSpecificRequirement.ts index 31cd6d3..5acb718 100644 --- a/src/extensions/functions/buildSpecificRequirement.ts +++ b/src/extensions/functions/buildSpecificRequirement.ts @@ -66,7 +66,7 @@ namespace buildSpecificRequirement { const constructRequirment = async context => { const { parameters, prompt, solidarity } = context const { getSolidaritySettings, ruleHandlers } = solidarity - const solidaritySettings = getSolidaritySettings(context) + const solidaritySettings = await getSolidaritySettings(context) const userAnswer = await prompt.ask({ name: 'addNewRule', diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index e631970..352358c 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -1,4 +1,4 @@ -import { SolidarityRunContext, SolidaritySettings, solidarity } from '../../types' +import { SolidarityRunContext, SolidaritySettings } from '../../types' import * as JSON5 from 'json5' import * as path from 'path' @@ -25,15 +25,31 @@ export const loadModule = (context, moduleName) => { } } -export const loadWebCheck = (context, checkStack) => { - -} - -export const loadDefault = () => { +export const loadWebCheck = async (context, checkOption) => { + const { print, http } = context + const checkSpinner = print.spin(`Running check on ${checkOption}`) + const api = http.create({ + baseURL: 'https://api.github.com', + headers: { Accept: 'application/vnd.github.v3+json' }, + }) + // Load check from web + const checkURL = `https:\/\/raw.githubusercontent.com/infinitered/solidarity/master/${checkOption}` + const result = await api.get(checkURL) + // console.log(result) + if (result.ok) { + checkSpinner.succeed(checkURL) + return result.data + } else { + checkSpinner.fail(`Unable to find a known check for ${checkOption}`) + print.info( + `https://github.com/infinitered/solidarity/checks for options.` + ) + throw(`ERROR: ${result.status} - ${result.problem}`) + } } -module.exports = (context: SolidarityRunContext): SolidaritySettings => { +module.exports = async (context: SolidarityRunContext): Promise => { const { filesystem, parameters } = context const options = parameters.options || {} // fix possibly undefined from gluegun const demandedFile = options.solidarityFile || options.f @@ -52,7 +68,7 @@ module.exports = (context: SolidarityRunContext): SolidaritySettings => { } else if (demandedModule) { solidaritySettings = loadModule(context, demandedModule) } else if (demandedCheck) { - solidaritySettings = loadWebCheck(context, demandedCheck) + solidaritySettings = await loadWebCheck(context, demandedCheck) } else if (filesystem.exists('.solidarity')) { solidaritySettings = JSON5.parse(filesystem.read('.solidarity')) } else if (filesystem.exists('.solidarity.json')) { diff --git a/src/extensions/functions/updateVersions.ts b/src/extensions/functions/updateVersions.ts index 20c6c39..3b99f28 100644 --- a/src/extensions/functions/updateVersions.ts +++ b/src/extensions/functions/updateVersions.ts @@ -6,7 +6,7 @@ module.exports = async (context: SolidarityRunContext): Promise => { const { getSolidaritySettings, setSolidaritySettings, updateRequirement } = solidarity // load current solidarity file - const solidaritySettings = getSolidaritySettings(context) + const solidaritySettings = await getSolidaritySettings(context) // Map over requirements with option to mutate settings const checks = await map( From 4c1ece75529026d403c1e31c9a6c41bbffd7446f Mon Sep 17 00:00:00 2001 From: Gant Date: Sun, 5 Aug 2018 13:30:08 -0500 Subject: [PATCH 04/19] basic solidarity setup --- checks/react-native.json | 31 +++++++++++++++++++ .../functions/getSolidaritySettings.ts | 15 +++++---- 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 checks/react-native.json diff --git a/checks/react-native.json b/checks/react-native.json new file mode 100644 index 0000000..55397cb --- /dev/null +++ b/checks/react-native.json @@ -0,0 +1,31 @@ +{ + "$schema": "http://json.schemastore.org/solidaritySchema", + "config" : { + "output" : "verbose" + }, + "requirements": { + "Node": [{ "rule": "cli", "binary": "node"}], + "Watchman": [ + { + "rule": "cli", + "binary": "watchman", + "error": "Please install watchman on this machine. Refer to the official Watchman installation instructions for additional help.", + "platform": ["darwin", "linux"] + } + ], + "React Native": [ + { "rule": "cli", "binary": "react-native" } + ], + "Android": [ + { + "rule": "env", + "variable": "ANDROID_HOME", + "error": "The ANDROID_HOME environment variable must be set to your local SDK. Refer to getting started docs for help." + } + ], + "Xcode": [ + { "rule": "cli", "binary": "xcodebuild", "platform": "darwin" }, + { "rule": "cli", "binary": "xcrun", "platform": "darwin" } + ] + } +} diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index 352358c..5e7c170 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -2,6 +2,8 @@ import { SolidarityRunContext, SolidaritySettings } from '../../types' import * as JSON5 from 'json5' import * as path from 'path' +export const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/) + export const loadFile = (context, filePath) => { const { filesystem } = context if (filesystem.exists(filePath)) { @@ -34,18 +36,19 @@ export const loadWebCheck = async (context, checkOption) => { }) // Load check from web - const checkURL = `https:\/\/raw.githubusercontent.com/infinitered/solidarity/master/${checkOption}` + const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity/master/checks/${checkOption}.json` const result = await api.get(checkURL) // console.log(result) if (result.ok) { - checkSpinner.succeed(checkURL) - return result.data + checkSpinner.succeed(`Found ${checkOption}`) + const solidarityData = JSON5.parse(result.data) + return solidarityData } else { - checkSpinner.fail(`Unable to find a known check for ${checkOption}`) + checkSpinner.fail(`Unable to find a known check stack for ${checkOption}`) print.info( - `https://github.com/infinitered/solidarity/checks for options.` + `Check https://github.com/infinitered/solidarity/checks for options.` ) - throw(`ERROR: ${result.status} - ${result.problem}`) + throw(`ERROR: Request failed (${result.status} - ${result.problem})`) } } From 1c0d35309d2870813f287ae2fb0dd9fcc926bd0c Mon Sep 17 00:00:00 2001 From: Gant Date: Sun, 5 Aug 2018 13:46:52 -0500 Subject: [PATCH 05/19] properly parse json5 strings --- checks/react-native.json | 10 +++++----- src/extensions/functions/getSolidaritySettings.ts | 5 ++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/checks/react-native.json b/checks/react-native.json index 55397cb..874d97a 100644 --- a/checks/react-native.json +++ b/checks/react-native.json @@ -4,8 +4,8 @@ "output" : "verbose" }, "requirements": { - "Node": [{ "rule": "cli", "binary": "node"}], - "Watchman": [ + "Have Node": [{ "rule": "cli", "binary": "node"}], + "Have Watchman": [ { "rule": "cli", "binary": "watchman", @@ -13,17 +13,17 @@ "platform": ["darwin", "linux"] } ], - "React Native": [ + "Have React Native": [ { "rule": "cli", "binary": "react-native" } ], - "Android": [ + "Have Android": [ { "rule": "env", "variable": "ANDROID_HOME", "error": "The ANDROID_HOME environment variable must be set to your local SDK. Refer to getting started docs for help." } ], - "Xcode": [ + "Have Xcode": [ { "rule": "cli", "binary": "xcodebuild", "platform": "darwin" }, { "rule": "cli", "binary": "xcrun", "platform": "darwin" } ] diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index 5e7c170..ce5fdd1 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -41,7 +41,10 @@ export const loadWebCheck = async (context, checkOption) => { // console.log(result) if (result.ok) { checkSpinner.succeed(`Found ${checkOption}`) - const solidarityData = JSON5.parse(result.data) + // Convert strings to JSON5 objects + const solidarityData = (typeof result.data === 'string') + ? JSON5.parse(result.data) + : result.data return solidarityData } else { checkSpinner.fail(`Unable to find a known check stack for ${checkOption}`) From ee65d6ddb1dda25328f48a77da78984e4d1e4bf2 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Tue, 7 Aug 2018 09:43:03 -0500 Subject: [PATCH 06/19] move check to stack --- src/extensions/functions/getSolidaritySettings.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index ce5fdd1..bcce66c 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -60,7 +60,7 @@ module.exports = async (context: SolidarityRunContext): Promise Date: Tue, 7 Aug 2018 15:45:12 -0500 Subject: [PATCH 07/19] now uses Solidarity Stacks repo --- src/extensions/functions/getSolidaritySettings.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index bcce66c..17c73bf 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -36,11 +36,11 @@ export const loadWebCheck = async (context, checkOption) => { }) // Load check from web - const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity/master/checks/${checkOption}.json` + const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity` const result = await api.get(checkURL) // console.log(result) if (result.ok) { - checkSpinner.succeed(`Found ${checkOption}`) + checkSpinner.succeed(`Found Stack: ${checkOption}`) // Convert strings to JSON5 objects const solidarityData = (typeof result.data === 'string') ? JSON5.parse(result.data) @@ -49,7 +49,7 @@ export const loadWebCheck = async (context, checkOption) => { } else { checkSpinner.fail(`Unable to find a known check stack for ${checkOption}`) print.info( - `Check https://github.com/infinitered/solidarity/checks for options.` + `Check https://github.com/infinitered/solidarity-stacks for options.` ) throw(`ERROR: Request failed (${result.status} - ${result.problem})`) } From 74998e8e9af1e4dc5596b100767a93fcb814bcb3 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Tue, 7 Aug 2018 16:29:07 -0500 Subject: [PATCH 08/19] fix shortcut and help --- package-lock.json | 14 +++++++++++++- package.json | 2 +- src/commands/help.ts | 2 +- src/extensions/functions/getSolidaritySettings.ts | 5 ++--- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3b610be..ed80fd9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "solidarity", - "version": "2.0.5", + "version": "2.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -157,6 +157,12 @@ "integrity": "sha1-CFQ0P/whQaSL9UGjr7tnz+B1Yqo=", "dev": true }, + "@types/cli-table2": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@types/cli-table2/-/cli-table2-0.2.1.tgz", + "integrity": "sha512-cy1KtTmPRXb9PAqINWztOdPZzvqae2dvENWlRbhRG3pYcK7qGtW/yAdyxt2xFTvyHz0kqsiXjIpn1574qyj9LA==", + "dev": true + }, "@types/execa": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@types/execa/-/execa-0.8.1.tgz", @@ -166,6 +172,12 @@ "@types/node": "*" } }, + "@types/jasmine": { + "version": "2.8.8", + "resolved": "https://registry.npmjs.org/@types/jasmine/-/jasmine-2.8.8.tgz", + "integrity": "sha512-OJSUxLaxXsjjhob2DBzqzgrkLmukM3+JMpRp0r0E4HTdT1nwDCWhaswjYxazPij6uOdzHCJfNbDjmQ1/rnNbCg==", + "dev": true + }, "@types/jest": { "version": "22.2.0", "resolved": "https://registry.npmjs.org/@types/jest/-/jest-22.2.0.tgz", diff --git a/package.json b/package.json index a9e17fe..9058e34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "solidarity", - "version": "2.1.0", + "version": "2.2.0", "description": "Make sure all React Native dependencies are uniform across machines", "repository": "https://github.com/infinitered/solidarity", "bin": { diff --git a/src/commands/help.ts b/src/commands/help.ts index ac44e94..46465af 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -15,7 +15,7 @@ module.exports = { print.info(' --silent\t (-s) No output, just a return code of success/failure') print.info(' --solidarityFile\t (-f) Use given path to solidarity file for settings') print.info(' --module\t (-m) Search for a solidarity file in the given npm package') - print.info(' --check\t (-c) Use a known technology, and not the local file') + print.info(' --stack\t (-t) Use a known technology stack, and not the local file') print.success(colors.magenta('\nSolidarity is open source - https://github.com/infinitered/solidarity')) print.info(colors.magenta('If you need additional help, join our Slack at http://community.infinite.red')) diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index 17c73bf..fcbf6a7 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -31,8 +31,7 @@ export const loadWebCheck = async (context, checkOption) => { const { print, http } = context const checkSpinner = print.spin(`Running check on ${checkOption}`) const api = http.create({ - baseURL: 'https://api.github.com', - headers: { Accept: 'application/vnd.github.v3+json' }, + baseURL: 'https://api.github.com' }) // Load check from web @@ -60,7 +59,7 @@ module.exports = async (context: SolidarityRunContext): Promise Date: Tue, 7 Aug 2018 22:40:54 -0500 Subject: [PATCH 09/19] verbiage chage --- src/extensions/functions/getSolidaritySettings.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index fcbf6a7..da9ff07 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -46,7 +46,7 @@ export const loadWebCheck = async (context, checkOption) => { : result.data return solidarityData } else { - checkSpinner.fail(`Unable to find a known check stack for ${checkOption}`) + checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`) print.info( `Check https://github.com/infinitered/solidarity-stacks for options.` ) From 35ae91048cbc03862a34b9383218e0cbb247588d Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 11:53:12 -0500 Subject: [PATCH 10/19] no more local file --- checks/react-native.json | 31 ------------------------------- 1 file changed, 31 deletions(-) delete mode 100644 checks/react-native.json diff --git a/checks/react-native.json b/checks/react-native.json deleted file mode 100644 index 874d97a..0000000 --- a/checks/react-native.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "$schema": "http://json.schemastore.org/solidaritySchema", - "config" : { - "output" : "verbose" - }, - "requirements": { - "Have Node": [{ "rule": "cli", "binary": "node"}], - "Have Watchman": [ - { - "rule": "cli", - "binary": "watchman", - "error": "Please install watchman on this machine. Refer to the official Watchman installation instructions for additional help.", - "platform": ["darwin", "linux"] - } - ], - "Have React Native": [ - { "rule": "cli", "binary": "react-native" } - ], - "Have Android": [ - { - "rule": "env", - "variable": "ANDROID_HOME", - "error": "The ANDROID_HOME environment variable must be set to your local SDK. Refer to getting started docs for help." - } - ], - "Have Xcode": [ - { "rule": "cli", "binary": "xcodebuild", "platform": "darwin" }, - { "rule": "cli", "binary": "xcrun", "platform": "darwin" } - ] - } -} From f3b32d5a561ff319bd147499c80940da86ae6314 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 12:40:28 -0500 Subject: [PATCH 11/19] fix folder flag --- src/extensions/functions/getSolidaritySettings.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index da9ff07..4f0572f 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -6,8 +6,12 @@ export const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/) export const loadFile = (context, filePath) => { const { filesystem } = context - if (filesystem.exists(filePath)) { - return JSON5.parse(filesystem.read('.solidarity')) + if (filesystem.exists(filePath + path.sep + '.solidarity')) { + return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity')) + } else if (filesystem.exists(filePath + path.sep + '.solidarity.json')) { + return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity.json')) + } else if (filesystem.exists(filePath) === 'file') { + return JSON5.parse(filesystem.read(filePath)) } else { throw 'ERROR: There is no solidarity file at the given path' } From 2edb1988975790416d592985445a9a9452524ce8 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 12:55:57 -0500 Subject: [PATCH 12/19] fixing up tests --- .../command_helpers/getSolidaritySettings.ts | 42 +++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/__tests__/command_helpers/getSolidaritySettings.ts b/__tests__/command_helpers/getSolidaritySettings.ts index 27105ad..69573e2 100644 --- a/__tests__/command_helpers/getSolidaritySettings.ts +++ b/__tests__/command_helpers/getSolidaritySettings.ts @@ -21,23 +21,31 @@ describe('basic getSolidaritySettings', () => { }) }) - // describe('w/ failure', () => { - // test('getSolidaritySettings can fail', async () => { - // await expect(async () => { - // process.chdir('__tests__') - // await getSolidaritySettings(context) - // }).toThrow() - // process.chdir('../') - // }) - - // test('getSolidaritySettings can warn with missing requirements', async () => { - // await expect(async () => { - // process.chdir('__tests__/sandbox/solidarity_broken') - // await getSolidaritySettings(context) - // }).toThrowError('ERROR: Found, but no requirements key. Please validate your solidarity file') - // process.chdir('../../../') - // }) - // }) + describe('w/ failure', () => { + test('getSolidaritySettings can fail', async () => { + + // Original sync style + // expect(async () => { + // process.chdir('__tests__') + // getSolidaritySettings(context) + // }).toThrow() + // process.chdir('../') + + process.chdir('__tests__') + await expect(getSolidaritySettings(context)) + .rejects + .toThrow() + process.chdir('../') + }) + + test('getSolidaritySettings can warn with missing requirements', async () => { + process.chdir('__tests__/sandbox/solidarity_broken') + await expect(getSolidaritySettings(context)) + .rejects + .toThrow() + process.chdir('../../../') + }) + }) }) describe('parameterized getSolidaritySettings', () => { From 749c1a9b6043216a334afb32dad5aa40073fe523 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 14:44:42 -0500 Subject: [PATCH 13/19] break out helpers and write tests --- .../command_helpers/getSolidarityHelpers.ts | 50 ++++++++++++++ .../command_helpers/getSolidaritySettings.ts | 2 +- .../functions/getSolidarityHelpers.ts | 66 +++++++++++++++++++ .../functions/getSolidaritySettings.ts | 58 +--------------- 4 files changed, 118 insertions(+), 58 deletions(-) create mode 100644 __tests__/command_helpers/getSolidarityHelpers.ts create mode 100644 src/extensions/functions/getSolidarityHelpers.ts diff --git a/__tests__/command_helpers/getSolidarityHelpers.ts b/__tests__/command_helpers/getSolidarityHelpers.ts new file mode 100644 index 0000000..4f02999 --- /dev/null +++ b/__tests__/command_helpers/getSolidarityHelpers.ts @@ -0,0 +1,50 @@ +import { isURI, loadFile, loadModule, loadWebCheck } from '../../src/extensions/functions/getSolidarityHelpers' + +const context = require('mockContext') + +describe('Test helper functions', () => { + describe('isURI', () => { + test('isURI positive case', () => { + expect(isURI('http://www.google.com')).toBeTruthy() + expect(isURI('https://www.google.com')).toBeTruthy() + }) + + test('isURI fail case', () => { + expect(isURI('nachos')).toBeFalsy() + expect(isURI('/nachos')).toBeFalsy() + expect(isURI('./nachos')).toBeFalsy() + }) + + }) + + describe('loadFile', () => { + test('loadFile positive cases', () => { + expect(loadFile(context, '__tests__/sandbox/solidarity_json')).toBeTruthy() + expect(loadFile(context, '__tests__/sandbox/solidarity_json/.solidarity.json')).toBeTruthy() + }) + + test('loadFile false cases', () => { + expect(() => { + loadFile(context, '__tests__/sandbox/fake_project') + }).toThrow() + expect(() => { + loadFile(context, '__tests__/sandbox/fake_project/nope.solidarity') + }).toThrow() + }) + }) + + // describe('loadModule', () => { + // }) + + describe('loadWebCheck', () => { + test('loadWebCheck positive cases', async () => { + expect(await loadWebCheck(context, 'https://raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/react-native.solidarity')).toBeTruthy() + }) + + test('loadWebCheck false cases', async () => { + await expect(loadWebCheck(context, 'https://raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/failsauce')) + .rejects + .toThrow() + }) + }) +}) diff --git a/__tests__/command_helpers/getSolidaritySettings.ts b/__tests__/command_helpers/getSolidaritySettings.ts index 69573e2..fdaa502 100644 --- a/__tests__/command_helpers/getSolidaritySettings.ts +++ b/__tests__/command_helpers/getSolidaritySettings.ts @@ -25,7 +25,7 @@ describe('basic getSolidaritySettings', () => { test('getSolidaritySettings can fail', async () => { // Original sync style - // expect(async () => { + // expect(() => { // process.chdir('__tests__') // getSolidaritySettings(context) // }).toThrow() diff --git a/src/extensions/functions/getSolidarityHelpers.ts b/src/extensions/functions/getSolidarityHelpers.ts new file mode 100644 index 0000000..8ef0e43 --- /dev/null +++ b/src/extensions/functions/getSolidarityHelpers.ts @@ -0,0 +1,66 @@ +import * as JSON5 from 'json5' +import * as path from 'path' + +const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/) + +const loadFile = (context, filePath) => { + const { filesystem } = context + if (filesystem.exists(filePath) === 'file') { + return JSON5.parse(filesystem.read(filePath)) + } else if (filesystem.exists(filePath + path.sep + '.solidarity')) { + return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity')) + } else if (filesystem.exists(filePath + path.sep + '.solidarity.json')) { + return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity.json')) + } else { + throw 'ERROR: There is no solidarity file at the given path' + } +} + +const loadModule = (context, moduleName) => { + const { filesystem } = context + // We will search that module + const filePath = path.join('node_modules', moduleName, '.solidarity') + + if (filesystem.exists(filePath)) { + return JSON5.parse(filesystem.read(filePath)) + } else if (filesystem.exists(filePath + '.json')) { + return JSON5.parse(filesystem.read(filePath + '.json')) + } else { + throw 'ERROR: There is no solidarity file found with the given module' + } +} + +const loadWebCheck = async (context, checkOption) => { + const { print, http } = context + const checkSpinner = print.spin(`Running check on ${checkOption}`) + // the base URL is throw away, and will go away in next version of apisauce + const api = http.create({ + baseURL: 'https://api.github.com' + }) + + // Load check from web + const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity` + const result = await api.get(checkURL) + // console.log(result) + if (result.ok) { + checkSpinner.succeed(`Found Stack: ${checkOption}`) + // Convert strings to JSON5 objects + const solidarityData = (typeof result.data === 'string') + ? JSON5.parse(result.data) + : result.data + return solidarityData + } else { + checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`) + print.info( + `Check https://github.com/infinitered/solidarity-stacks for options.` + ) + throw(`ERROR: Request failed (${result.status} - ${result.problem})`) + } +} + +module.exports = { + isURI, + loadFile, + loadModule, + loadWebCheck +} diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index 4f0572f..b6d8b6f 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -1,62 +1,6 @@ import { SolidarityRunContext, SolidaritySettings } from '../../types' +import { loadFile, loadModule, loadWebCheck } from './getSolidarityHelpers' import * as JSON5 from 'json5' -import * as path from 'path' - -export const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/) - -export const loadFile = (context, filePath) => { - const { filesystem } = context - if (filesystem.exists(filePath + path.sep + '.solidarity')) { - return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity')) - } else if (filesystem.exists(filePath + path.sep + '.solidarity.json')) { - return JSON5.parse(filesystem.read(filePath + path.sep + '.solidarity.json')) - } else if (filesystem.exists(filePath) === 'file') { - return JSON5.parse(filesystem.read(filePath)) - } else { - throw 'ERROR: There is no solidarity file at the given path' - } -} - -export const loadModule = (context, moduleName) => { - const { filesystem } = context - // We will search that module - const filePath = path.join('node_modules', moduleName, '.solidarity') - - if (filesystem.exists(filePath)) { - return JSON5.parse(filesystem.read(filePath)) - } else if (filesystem.exists(filePath + '.json')) { - return JSON5.parse(filesystem.read(filePath + '.json')) - } else { - throw 'ERROR: There is no solidarity file found with the given module' - } -} - -export const loadWebCheck = async (context, checkOption) => { - const { print, http } = context - const checkSpinner = print.spin(`Running check on ${checkOption}`) - const api = http.create({ - baseURL: 'https://api.github.com' - }) - - // Load check from web - const checkURL = isURI(checkOption) ? checkOption : `https:\/\/raw.githubusercontent.com/infinitered/solidarity-stacks/master/stacks/${checkOption}.solidarity` - const result = await api.get(checkURL) - // console.log(result) - if (result.ok) { - checkSpinner.succeed(`Found Stack: ${checkOption}`) - // Convert strings to JSON5 objects - const solidarityData = (typeof result.data === 'string') - ? JSON5.parse(result.data) - : result.data - return solidarityData - } else { - checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`) - print.info( - `Check https://github.com/infinitered/solidarity-stacks for options.` - ) - throw(`ERROR: Request failed (${result.status} - ${result.problem})`) - } -} module.exports = async (context: SolidarityRunContext): Promise => { const { filesystem, parameters } = context From b1f2e0f0b824bfd2bcdf88741fb97080c3f85710 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 14:51:03 -0500 Subject: [PATCH 14/19] fix for tsc --- src/extensions/functions/getSolidarityHelpers.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/extensions/functions/getSolidarityHelpers.ts b/src/extensions/functions/getSolidarityHelpers.ts index 8ef0e43..7aa34bf 100644 --- a/src/extensions/functions/getSolidarityHelpers.ts +++ b/src/extensions/functions/getSolidarityHelpers.ts @@ -1,9 +1,9 @@ import * as JSON5 from 'json5' import * as path from 'path' -const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/) +export const isURI = (path) => !!path.match(/\w+:(\/?\/?)[^\s]+/) -const loadFile = (context, filePath) => { +export const loadFile = (context, filePath) => { const { filesystem } = context if (filesystem.exists(filePath) === 'file') { return JSON5.parse(filesystem.read(filePath)) @@ -16,7 +16,7 @@ const loadFile = (context, filePath) => { } } -const loadModule = (context, moduleName) => { +export const loadModule = (context, moduleName) => { const { filesystem } = context // We will search that module const filePath = path.join('node_modules', moduleName, '.solidarity') @@ -30,7 +30,7 @@ const loadModule = (context, moduleName) => { } } -const loadWebCheck = async (context, checkOption) => { +export const loadWebCheck = async (context, checkOption) => { const { print, http } = context const checkSpinner = print.spin(`Running check on ${checkOption}`) // the base URL is throw away, and will go away in next version of apisauce From 92251381c978154c1e9f270b76cd706a33a82219 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 14:57:56 -0500 Subject: [PATCH 15/19] no more check folder --- .npmignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.npmignore b/.npmignore index e4ee23d..d392179 100644 --- a/.npmignore +++ b/.npmignore @@ -6,4 +6,3 @@ src/ coverage/ yarn.lock *.log -check/ From fbc877a86d68ce1ab0346535dca7c87fd6164e73 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 16:05:36 -0500 Subject: [PATCH 16/19] adding CLI Options docs --- docs/_sidebar.md | 1 + docs/cliOptions.md | 55 ++++++++++++++++++++++++++++++++++++++++++++++ docs/options.md | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 docs/cliOptions.md diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 6d4c48a..4746254 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -10,6 +10,7 @@ - [Additional Info](/README.md#additional-info) - Comprehensive Docs - [Solidarity Rules Options](/docs/options.md) + - [Solidarity CLI Options](/docs/cliOptions.md) - [Writing Plugins](/docs/plugins.md) - [**All Contributors**](/docs/existingContributors.md) - Blog Posts diff --git a/docs/cliOptions.md b/docs/cliOptions.md new file mode 100644 index 0000000..b48a02a --- /dev/null +++ b/docs/cliOptions.md @@ -0,0 +1,55 @@ +# Solidarity Command Options +A listing of CLI options can be found by passing the `help` command to the CLI. + +``` + $ solidarity help + +Solidarity + Commands + + solidarity Check environment against solidarity rules + create (c) Displays this help + help (h) Displays this help + report (r) Report solidarity info about the current machine + snapshot (s) Take a snapshot of the versions and store in solidarity file + + Flags + + --verbose (-a) Prints all detected info during solidarity check + --moderate (-m) Prints failures in check or single success message + --silent (-s) No output, just a return code of success/failure + --solidarityFile (-f) Use given path to solidarity file for settings + --module (-m) Search for a solidarity file in the given npm package + --stack (-t) Use a known technology stack, and not the local file + +Solidarity is open source - https://github.com/infinitered/solidarity +If you need additional help, join our Slack at http://community.infinite.red +``` + +Here we will go into detail on each option flag. + +## verbose (-a) +Passing `--verbose` or `-a` flags will modify output to be verbose. + +## moderate (-m) +Passing `--moderate` or `-m` flags will modify output to be moderate, meaning only failures exclusive or a single success will be printed. + +## silent (-s) +Passing `--silent` or `-s` flags will modify output to be silent, meaning no output will occur. You'll have to see if the command return is non-zero to see if it failed. + +## solidarityFile (-f) +Passing `--solidarityFile` or `-f` flags will direct the file to use for the solidarity check. + +> For example: `solidarity -solidarityFile ./my/special/file.json` will run the designated file instead of looking for a local folder Solidarity file. + +## module (-m) +Passing `--module` or `-m` flags will modify the designated solidarity file, to run a file found in the given `node_module` stack. + +> For example: `solidarity --module smoothReporter` will run the solidarity file in the root of the npm package `smoothReporter` instead of our own. + +## stack (-t) +Passing `--stack` or `-t` flags will make our stack look to GitHub for a well known tech stack. + +> For example: `solidarity --stack react-native` will check our machine if we are ready to run React Native projects, but not a specific React Native project. + +Stacks are community managed and found here: https://github.com/infinitered/solidarity-stacks diff --git a/docs/options.md b/docs/options.md index 1bb0fc7..7b4f0ad 100644 --- a/docs/options.md +++ b/docs/options.md @@ -1,4 +1,4 @@ -# Solidarity Options +# Solidarity File Options Understanding the `.solidarity` file helps you read and write new solidarity checks for any project. If you'd like to get auto-complete, or validation of your JSON rules, you can optionally add the following line to the `.solidarity` file for help in your personal editor. From b87e22b221f00e4ae7bb95efc5eac408b8f7d614 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 16:43:49 -0500 Subject: [PATCH 17/19] fix silent bug --- src/commands/solidarity.ts | 1 + .../functions/getSolidarityHelpers.ts | 22 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/commands/solidarity.ts b/src/commands/solidarity.ts index d0bd0e8..c16a8f2 100644 --- a/src/commands/solidarity.ts +++ b/src/commands/solidarity.ts @@ -58,6 +58,7 @@ namespace Solidarity { process.exit(3) } + // Merge flags and configs context.outputMode = setOutputMode(context.parameters, solidaritySettings) // build map of checks to perform diff --git a/src/extensions/functions/getSolidarityHelpers.ts b/src/extensions/functions/getSolidarityHelpers.ts index 7aa34bf..8a120ed 100644 --- a/src/extensions/functions/getSolidarityHelpers.ts +++ b/src/extensions/functions/getSolidarityHelpers.ts @@ -1,3 +1,6 @@ +import { + SolidarityOutputMode, +} from '../../types' import * as JSON5 from 'json5' import * as path from 'path' @@ -31,8 +34,11 @@ export const loadModule = (context, moduleName) => { } export const loadWebCheck = async (context, checkOption) => { - const { print, http } = context - const checkSpinner = print.spin(`Running check on ${checkOption}`) + const { print, http, parameters } = context + const { options } = parameters + const silentMode = options.silent || options.s + const moderateMode = options.moderate || options.m + const checkSpinner = silentMode || moderateMode ? null : print.spin(`Running check on ${checkOption}`) // the base URL is throw away, and will go away in next version of apisauce const api = http.create({ baseURL: 'https://api.github.com' @@ -43,17 +49,19 @@ export const loadWebCheck = async (context, checkOption) => { const result = await api.get(checkURL) // console.log(result) if (result.ok) { - checkSpinner.succeed(`Found Stack: ${checkOption}`) + checkSpinner && checkSpinner.succeed(`Found Stack: ${checkOption}`) // Convert strings to JSON5 objects const solidarityData = (typeof result.data === 'string') ? JSON5.parse(result.data) : result.data return solidarityData } else { - checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`) - print.info( - `Check https://github.com/infinitered/solidarity-stacks for options.` - ) + checkSpinner && checkSpinner.fail(`Unable to find a known tech stack for ${checkOption}`) + if (!silentMode) { + print.info( + `Check https://github.com/infinitered/solidarity-stacks for options.` + ) + } throw(`ERROR: Request failed (${result.status} - ${result.problem})`) } } From 6b86542dffc97a5d288ea40b6434660ce61c0d43 Mon Sep 17 00:00:00 2001 From: Gant Laborde Date: Fri, 10 Aug 2018 16:47:01 -0500 Subject: [PATCH 18/19] fix naming collision --- docs/cliOptions.md | 10 +++++----- src/commands/help.ts | 10 +++++----- src/extensions/functions/getSolidaritySettings.ts | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/cliOptions.md b/docs/cliOptions.md index b48a02a..a45fa30 100644 --- a/docs/cliOptions.md +++ b/docs/cliOptions.md @@ -15,12 +15,12 @@ Solidarity Flags - --verbose (-a) Prints all detected info during solidarity check - --moderate (-m) Prints failures in check or single success message - --silent (-s) No output, just a return code of success/failure + --verbose (-a) Prints all detected info during solidarity check + --moderate (-m) Prints failures in check or single success message + --silent (-s) No output, just a return code of success/failure --solidarityFile (-f) Use given path to solidarity file for settings - --module (-m) Search for a solidarity file in the given npm package - --stack (-t) Use a known technology stack, and not the local file + --module (-d) Search for a solidarity file in the given npm package + --stack (-t) Use a known technology stack, and not the local file Solidarity is open source - https://github.com/infinitered/solidarity If you need additional help, join our Slack at http://community.infinite.red diff --git a/src/commands/help.ts b/src/commands/help.ts index 46465af..9cc1a54 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -10,12 +10,12 @@ module.exports = { print.info(' Commands') print.printCommands(context) print.info('\n Flags\n') - print.info(' --verbose\t (-a) Prints all detected info during solidarity check') - print.info(' --moderate\t (-m) Prints failures in check or single success message') - print.info(' --silent\t (-s) No output, just a return code of success/failure') + print.info(' --verbose\t\t (-a) Prints all detected info during solidarity check') + print.info(' --moderate\t\t (-m) Prints failures in check or single success message') + print.info(' --silent\t\t (-s) No output, just a return code of success/failure') print.info(' --solidarityFile\t (-f) Use given path to solidarity file for settings') - print.info(' --module\t (-m) Search for a solidarity file in the given npm package') - print.info(' --stack\t (-t) Use a known technology stack, and not the local file') + print.info(' --module\t\t (-d) Search for a solidarity file in the given npm package') + print.info(' --stack\t\t (-t) Use a known technology stack, and not the local file') print.success(colors.magenta('\nSolidarity is open source - https://github.com/infinitered/solidarity')) print.info(colors.magenta('If you need additional help, join our Slack at http://community.infinite.red')) diff --git a/src/extensions/functions/getSolidaritySettings.ts b/src/extensions/functions/getSolidaritySettings.ts index b6d8b6f..6991a81 100644 --- a/src/extensions/functions/getSolidaritySettings.ts +++ b/src/extensions/functions/getSolidaritySettings.ts @@ -6,7 +6,7 @@ module.exports = async (context: SolidarityRunContext): Promise Date: Fri, 10 Aug 2018 16:59:00 -0500 Subject: [PATCH 19/19] fix up tests --- __tests__/command_helpers/getSolidarityHelpers.ts | 2 +- __tests__/command_helpers/getSolidaritySettings.ts | 4 ++-- src/extensions/functions/getSolidarityHelpers.ts | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/__tests__/command_helpers/getSolidarityHelpers.ts b/__tests__/command_helpers/getSolidarityHelpers.ts index 4f02999..a3b8d17 100644 --- a/__tests__/command_helpers/getSolidarityHelpers.ts +++ b/__tests__/command_helpers/getSolidarityHelpers.ts @@ -1,4 +1,4 @@ -import { isURI, loadFile, loadModule, loadWebCheck } from '../../src/extensions/functions/getSolidarityHelpers' +import { isURI, loadFile, loadWebCheck } from '../../src/extensions/functions/getSolidarityHelpers' const context = require('mockContext') diff --git a/__tests__/command_helpers/getSolidaritySettings.ts b/__tests__/command_helpers/getSolidaritySettings.ts index fdaa502..506f006 100644 --- a/__tests__/command_helpers/getSolidaritySettings.ts +++ b/__tests__/command_helpers/getSolidaritySettings.ts @@ -86,8 +86,8 @@ describe('parameterized getSolidaritySettings', () => { process.chdir('__tests__/sandbox/fake_project') }) - test('can find solidarity file in module with flag -m', async () => { - context.parameters.options = { m: 'mock_module' } + test('can find solidarity file in module with flag -d', async () => { + context.parameters.options = { d: 'mock_module' } const resultSettings = await getSolidaritySettings(context) // we got an object with requirements defined expect(resultSettings).toMatchObject({ requirements: {} }) diff --git a/src/extensions/functions/getSolidarityHelpers.ts b/src/extensions/functions/getSolidarityHelpers.ts index 8a120ed..5f8d2d5 100644 --- a/src/extensions/functions/getSolidarityHelpers.ts +++ b/src/extensions/functions/getSolidarityHelpers.ts @@ -1,6 +1,3 @@ -import { - SolidarityOutputMode, -} from '../../types' import * as JSON5 from 'json5' import * as path from 'path'