-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ruleset-migrator): resolve npm packages correctly (#2022)
- Loading branch information
Showing
13 changed files
with
231 additions
and
21 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
93 changes: 93 additions & 0 deletions
93
packages/ruleset-migrator/src/__tests__/ruleset.jest.test.ts
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,93 @@ | ||
import * as path from '@stoplight/path'; | ||
import * as fs from 'fs'; | ||
import { migrateRuleset } from '../index'; | ||
|
||
describe('migrator', () => { | ||
describe('node_modules resolution', () => { | ||
const cwd = path.join(__dirname, '.cache'); | ||
const name = 'my-npm-ruleset'; | ||
|
||
beforeAll(async () => { | ||
const pkgRoot = path.join(cwd, 'node_modules', name); | ||
await fs.promises.mkdir(pkgRoot, { recursive: true }); | ||
await fs.promises.writeFile( | ||
path.join(pkgRoot, 'package.json'), | ||
JSON.stringify({ | ||
name, | ||
main: './index.json', | ||
}), | ||
); | ||
await fs.promises.writeFile( | ||
path.join(pkgRoot, 'index.json'), | ||
JSON.stringify({ | ||
functions: ['test'], | ||
rules: { | ||
test: { | ||
given: '$', | ||
then: { | ||
function: 'test', | ||
}, | ||
}, | ||
}, | ||
}), | ||
); | ||
}); | ||
|
||
afterAll(async () => { | ||
await fs.promises.rmdir(cwd, { recursive: true }); | ||
}); | ||
|
||
it('should be supported', async () => { | ||
await fs.promises.writeFile( | ||
path.join(cwd, 'my-ruleset.json'), | ||
JSON.stringify({ | ||
extends: [name], | ||
formats: ['oas2'], | ||
rules: { | ||
rule: { | ||
then: { | ||
given: '$', | ||
function: 'truthy', | ||
}, | ||
}, | ||
}, | ||
}), | ||
); | ||
|
||
expect( | ||
await migrateRuleset(path.join(cwd, 'my-ruleset.json'), { | ||
format: 'esm', | ||
fs: { | ||
promises: { | ||
readFile: fs.promises.readFile, | ||
}, | ||
}, | ||
}), | ||
).toEqual(`import {oas2} from "@stoplight/spectral-formats"; | ||
import {truthy} from "@stoplight/spectral-functions"; | ||
import test from "${cwd}/node_modules/my-npm-ruleset/functions/test.js"; | ||
export default { | ||
"extends": [{ | ||
"rules": { | ||
"test": { | ||
"given": "$", | ||
"then": { | ||
"function": test | ||
} | ||
} | ||
} | ||
}], | ||
"formats": [oas2], | ||
"rules": { | ||
"rule": { | ||
"then": { | ||
"given": "$", | ||
"function": truthy | ||
} | ||
} | ||
} | ||
}; | ||
`); | ||
}); | ||
}); | ||
}); |
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
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
19 changes: 19 additions & 0 deletions
19
packages/ruleset-migrator/src/utils/__tests__/isKnownNpmRegistry.spec.ts
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,19 @@ | ||
import { isKnownNpmRegistry } from '../isKnownNpmRegistry'; | ||
|
||
describe('isKnownNmRegistry util', () => { | ||
it.each([ | ||
'https://unpkg.com/spectral-aws-apigateway-ruleset', | ||
'https://unpkg.com/spectral-aws-apigateway-ruleset/functions/draft4.js', | ||
'https://cdn.skypack.dev/@stoplight/spectral-core', | ||
])('given recognized %s registry, should return true', input => { | ||
expect(isKnownNpmRegistry(input)).toBe(true); | ||
}); | ||
|
||
it.each([ | ||
'ftp://unpkg.com/spectral-aws-apigateway-ruleset', | ||
'/nimma/legacy', | ||
'https://baz.unpkg.com/spectral-aws-apigateway-ruleset', | ||
])('given unrecognized %s entity, should return false', input => { | ||
expect(isKnownNpmRegistry(input)).toBe(false); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
packages/ruleset-migrator/src/utils/__tests__/isPackageImport.spec.ts
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,23 @@ | ||
import { isPackageImport } from '../isPackageImport'; | ||
|
||
describe('isPackageImport util', () => { | ||
it.each([ | ||
'nimma/legacy', | ||
'nimma', | ||
'lodash', | ||
'lodash/get', | ||
'lodash/get.js', | ||
'@stoplight/path', | ||
'@stoplight/spectral-core', | ||
'@stoplight/spectral-core/dist/file.js', | ||
])('given valid %s package import, should return true', input => { | ||
expect(isPackageImport(input)).toBe(true); | ||
}); | ||
|
||
it.each(['', 'spectral:oas', '/nimma/legacy', 'path', 'https://cdn.skypack.dev/@stoplight/spectral-core'])( | ||
'given invalid %s import, should return false', | ||
input => { | ||
expect(isPackageImport(input)).toBe(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { parse } from '@stoplight/path'; | ||
|
||
const KNOWN_PROVIDERS = ['unpkg.com', 'cdn.skypack.dev']; | ||
|
||
export function isKnownNpmRegistry(uri: string): boolean { | ||
const { protocol, origin } = parse(uri); | ||
if (origin === null) { | ||
return false; | ||
} | ||
|
||
if (protocol !== 'http' && protocol !== 'https') { | ||
return false; | ||
} | ||
|
||
return KNOWN_PROVIDERS.includes(origin); | ||
} |
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,12 @@ | ||
import validate from 'validate-npm-package-name'; | ||
|
||
const isValidPackageName = (packageName: string): boolean => validate(packageName).validForNewPackages; | ||
|
||
export const isPackageImport = (packageName: string): boolean => { | ||
const fragments = packageName.split('/'); | ||
if (packageName.startsWith('@') && fragments.length >= 2) { | ||
fragments.splice(0, 2, `${fragments[0]}/${fragments[1]}`); | ||
} | ||
|
||
return fragments.every(isValidPackageName); | ||
}; |
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