Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: adds better validation #116

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@
"author": "Jeff Wainwright <yowainwright@gmail.com> (https://jeffry.in)",
"license": "MIT",
"dependencies": {
"@types/validate-npm-package-name": "^4.0.2",
"commander": "^11.1.0",
"cosmiconfig": "^9.0.0",
"execa": "^8.0.1",
"fast-glob": "^3.3.2",
"gradient-string": "^2.0.2",
"ora": "8.0.1",
"rimraf": "^5.0.5"
"rimraf": "^5.0.5",
"validate-npm-package-name": "^5.0.0"
},
"devDependencies": {
"@commitlint/cli": "^18.6.1",
Expand Down
26 changes: 23 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/scripts/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFileSync, writeFileSync } from 'fs'
import validatePackageName from 'validate-npm-package-name'
import { execa } from 'execa'
import fg from 'fast-glob'
const { sync: glob } = fg
Expand Down Expand Up @@ -27,6 +28,7 @@ export const constructVersionMap = async ({
debug = false,
yarnConfig = false,
isTesting = false,
validate = validatePackageName,
}: ConstructVersionMapOptions) => {
const updatedCodeDependencies = await Promise.all(
codependencies.map(async (item) => {
Expand All @@ -35,19 +37,23 @@ export const constructVersionMap = async ({
return item
} else if (typeof item === 'string' && item.length > 1 && !item.includes(' ')) {
// the following 2 lines capture only accepted npm package names
const isModuleSafeCharacters = /^[A-Za-z0-9\-_.]+$/.test(item)
if (!isModuleSafeCharacters) throw 'invalid item'
const { validForNewPackages, validForOldPackages, errors } = validate(item)
const isValid = [validForNewPackages, validForOldPackages].every((valid) => valid === true)
if (!isValid) throw new Error(errors?.join(', '))

const runner = !yarnConfig ? 'npm' : 'yarn'
const cmd = !yarnConfig
? ['view', item, 'version', 'latest']
: ['npm', 'info', item, '--fields', 'version', '--json']
const { stdout = '' } = (await exec(runner, cmd)) as unknown as Record<string, string>

const version = !yarnConfig ? stdout.replace('\n', '') : JSON.parse(stdout.replace('\n', ''))?.version

if (version) return { [item]: version }

throw `${version}`
} else {
throw 'invalid item'
throw 'invalid item type'
}
} catch (err) {
if (debug)
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@
message?: string
err?: string
isDebugging?: boolean
obj?: any

Check warning on line 89 in src/types.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
}

export type ConstructVersionMapOptions = {
codependencies: CodeDependencies
exec?: any

Check warning on line 94 in src/types.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
debug?: boolean
yarnConfig?: boolean
isTesting?: boolean
validate?: any

Check warning on line 98 in src/types.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
}
19 changes: 12 additions & 7 deletions test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@ const {
checkFiles,
} = scripts

vi.mock('../src/script', () => {
const scripts = {
execPromise: vi.fn(),
}
return scripts
})

test('constructVersionMap => pass', async () => {
const exec = vi.fn(() => ({
stdout: '4.0.0',
stderr: '',
})) as any
const validate = vi.fn(() => ({
validForNewPackages: true,
validForOldPackages: true,
errors: [],
}))
const result = await constructVersionMap({
codependencies: ['lodash'],
exec,
validate,
})
expect(result).toEqual({ lodash: '4.0.0' })
})
Expand All @@ -35,10 +34,16 @@ test('constructVersionMap => fail', async () => {
stdout: '',
stderr: '',
})) as any
const validate = vi.fn(() => ({
validForNewPackages: false,
validForOldPackages: true,
errors: ['foo-bop', 'foo-beep'],
}))
const result = await constructVersionMap({
codependencies: ['lodash'],
exec,
isTesting: true,
validate,
})
expect(result).toEqual({})
})
Expand Down
Loading
Loading