-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add glob matching to excludes list (#11)
- Loading branch information
Showing
12 changed files
with
322 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ src | |
tsconfig.build.json | ||
tsconfig.json | ||
jest.config.ts | ||
.prettierrc.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "es5", | ||
"semi": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
declare module "fuzzy-search"; | ||
declare module 'fuzzy-search' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,44 @@ | ||
#!/usr/bin/env node | ||
|
||
import child_process from "child_process"; | ||
import { Command } from "commander"; | ||
import { readFileSync } from "fs"; | ||
import path from "path"; | ||
import { cwd } from "process"; | ||
import { Config, Options } from "./types"; | ||
import { presentOptions } from "./presentOptions"; | ||
|
||
const program = new Command(); | ||
|
||
async function run(options: Record<string, string | boolean | string[]> = {}) { | ||
const { config: configPath = "." } = options; | ||
import child_process from 'child_process' | ||
import { Command } from 'commander' | ||
import { readFileSync } from 'fs' | ||
import path from 'path' | ||
import { cwd } from 'process' | ||
import { Config } from './types' | ||
import { presentOptions } from './presentOptions' | ||
import { parseOptions } from './parseOptions' | ||
|
||
const program = new Command() | ||
|
||
async function run( | ||
cliOptions: Record<string, string | boolean | string[]> = {} | ||
) { | ||
const { config: configPath = '.' } = cliOptions | ||
|
||
const { default: config } = (await import( | ||
path.join( | ||
path.resolve(cwd(), configPath as string), | ||
"/.scriptscli.config.mjs", | ||
'/.scriptscli.config.mjs' | ||
) | ||
).catch(() => ({ options: {} }))) as { default: Config }; | ||
|
||
const packageJson = readFileSync(path.join(`${cwd()}/package.json`)); | ||
const data = JSON.parse(packageJson.toString()); | ||
).catch(() => ({ options: {} }))) as { default: Config | undefined } | ||
|
||
const npmScripts = Object.keys(data.scripts) | ||
.filter((script) => { | ||
if (!config) return true; | ||
return !config.exclude?.includes(script); | ||
}) | ||
.reduce((acc: Options, script) => { | ||
acc[script] = {}; | ||
return acc; | ||
}, {}); | ||
const packageJson = readFileSync(path.join(`${cwd()}/package.json`)) | ||
const data = JSON.parse(packageJson.toString()) | ||
|
||
try { | ||
const { cmd, args } = await presentOptions({ | ||
...npmScripts, | ||
...config?.options, | ||
}); | ||
child_process.spawn(cmd + " " + args, { stdio: "inherit", shell: true }); | ||
const options = parseOptions(config, data.scripts) | ||
const { cmd, args } = await presentOptions(options) | ||
child_process.spawn(cmd + ' ' + args, { stdio: 'inherit', shell: true }) | ||
} catch (e) { | ||
return; | ||
return | ||
} | ||
} | ||
|
||
program | ||
.action(run) | ||
.option( | ||
"-c,--config <FilePath>", | ||
"location of config file other than project root", | ||
); | ||
program.parse(process.argv); | ||
'-c,--config <FilePath>', | ||
'location of config file other than project root' | ||
) | ||
program.parse(process.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { parseOptions } from './parseOptions' | ||
import { Config } from './types' | ||
|
||
const npmScripts = { first: 'test', second: 'test' } | ||
|
||
describe('parseOptions', () => { | ||
describe('options', () => { | ||
it('should return all npm scripts if no options override', () => { | ||
const config: Config = { options: {} } | ||
|
||
const result = parseOptions(config, npmScripts) | ||
|
||
expect(result).toStrictEqual({ first: {}, second: {} }) | ||
}) | ||
|
||
it('should allow config to be undefined', () => { | ||
const result = parseOptions(undefined, npmScripts) | ||
|
||
expect(result).toStrictEqual({ first: {}, second: {} }) | ||
}) | ||
|
||
it('should combine config and npm scripts', () => { | ||
const config: Config = { options: { third: { exec: 'testing' } } } | ||
|
||
const result = parseOptions(config, npmScripts) | ||
|
||
expect(result).toStrictEqual({ | ||
first: {}, | ||
second: {}, | ||
third: { exec: 'testing' }, | ||
}) | ||
}) | ||
|
||
it('should use config to override npm scripts', () => { | ||
const config: Config = { options: { second: { exec: 'testing' } } } | ||
|
||
const result = parseOptions(config, npmScripts) | ||
|
||
expect(result).toStrictEqual({ | ||
first: {}, | ||
second: { exec: 'testing' }, | ||
}) | ||
}) | ||
}) | ||
describe('exclude', () => { | ||
it('should not exclude any when exclude is omitted', () => { | ||
const config: Config = {} | ||
|
||
const result = parseOptions(config, npmScripts) | ||
|
||
expect(result).toStrictEqual({ | ||
first: {}, | ||
second: {}, | ||
}) | ||
}) | ||
|
||
it('should exclude full name matches', () => { | ||
const config: Config = { exclude: ['first'] } | ||
|
||
const result = parseOptions(config, npmScripts) | ||
|
||
expect(result).toStrictEqual({ | ||
second: {}, | ||
}) | ||
}) | ||
|
||
it('should exclude glob matches', () => { | ||
const config: Config = { exclude: ['fi*'] } | ||
|
||
const result = parseOptions(config, npmScripts) | ||
|
||
expect(result).toStrictEqual({ | ||
second: {}, | ||
}) | ||
}) | ||
|
||
it('should not filter out config options', () => { | ||
const config: Config = { | ||
exclude: ['fi*'], | ||
options: { final: { exec: 'testing' } }, | ||
} | ||
|
||
const result = parseOptions(config, npmScripts) | ||
|
||
expect(result).toStrictEqual({ | ||
second: {}, | ||
final: { exec: 'testing' }, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Config, Options } from './types' | ||
|
||
export function parseOptions( | ||
config: Config | undefined, | ||
scripts: Record<string, string> | ||
): Options { | ||
const { names, globs } = (config?.exclude ?? []).reduce( | ||
(acc: { names: string[]; globs: string[] }, exc) => { | ||
if (exc.includes('*')) acc.globs.push(exc.replace(/\*/g, '')) | ||
else acc.names.push(exc) | ||
return acc | ||
}, | ||
{ names: [], globs: [] } | ||
) | ||
|
||
const npmScripts = Object.keys(scripts) | ||
.filter((script) => { | ||
if (!config || !config.exclude) return true | ||
|
||
let globMatch = false | ||
for (const glob of globs) { | ||
globMatch = script.startsWith(glob) | ||
if (globMatch) break | ||
} | ||
|
||
if (globMatch) return false | ||
|
||
return !names.includes(script) | ||
}) | ||
.reduce((acc: Options, script) => { | ||
acc[script] = {} | ||
return acc | ||
}, {}) | ||
|
||
return { ...npmScripts, ...config?.options } | ||
} |
Oops, something went wrong.