-
-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
misc(generator): Allow local paths to generators (#265)
* misc(generator): allow local paths to generators * misc(generator): allow local paths to generators * misc(generator): allow local paths to generators * misc(generator): allow local paths to generators
- Loading branch information
1 parent
d5c287b
commit 8011b5a
Showing
8 changed files
with
200 additions
and
18 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"use strict"; | ||
|
||
const fs = require("fs"); | ||
const path = require("path"); | ||
|
||
/** | ||
* Attempts to detect whether the string is a local path regardless of its | ||
* existence by checking its format. The point is to distinguish between | ||
* paths and modules on the npm registry. This will fail for non-existent | ||
* local Windows paths that begin with a drive letter, e.g. C:..\generator.js, | ||
* but will succeed for any existing files and any absolute paths. | ||
* | ||
* @param {String} str - string to check | ||
* @returns {Boolean} whether the string could be a path to a local file or directory | ||
*/ | ||
|
||
module.exports = function(str) { | ||
return path.isAbsolute(str) || /^\./.test(str) || fs.existsSync(str); | ||
}; |
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,24 @@ | ||
"use strict"; | ||
|
||
const isLocalPath = require("./is-local-path"); | ||
const path = require("path"); | ||
|
||
describe("is-local-path", () => { | ||
it("returns true for paths beginning in the current directory", () => { | ||
const p = path.resolve(".", "test", "dir"); | ||
expect(isLocalPath(p)).toBe(true); | ||
}); | ||
|
||
it("returns true for absolute paths", () => { | ||
const p = path.join("/", "test", "dir"); | ||
expect(isLocalPath(p)).toBe(true); | ||
}); | ||
|
||
it("returns false for npm packages names", () => { | ||
expect(isLocalPath("webpack-addons-ylvis")).toBe(false); | ||
}); | ||
|
||
it("returns false for scoped npm package names", () => { | ||
expect(isLocalPath("@webpack/test")).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
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 @@ | ||
const npmPackagesExists = require("./npm-packages-exists"); | ||
|
||
jest.mock("./npm-exists"); | ||
jest.mock("./resolve-packages"); | ||
|
||
const mockResolvePackages = require("./resolve-packages").resolvePackages; | ||
|
||
describe("npmPackagesExists", () => { | ||
test("resolves packages when they are available on the local filesystem", () => { | ||
npmPackagesExists(["./testpkg"]); | ||
expect( | ||
mockResolvePackages.mock.calls[ | ||
mockResolvePackages.mock.calls.length - 1 | ||
][0] | ||
).toEqual(["./testpkg"]); | ||
}); | ||
|
||
test("throws a TypeError when an npm package name doesn't include the prefix", () => { | ||
expect(() => npmPackagesExists(["my-webpack-addon"])).toThrowError( | ||
TypeError | ||
); | ||
}); | ||
|
||
test("resolves packages when they are available on npm", done => { | ||
require("./npm-exists").mockImplementation(() => Promise.resolve(true)); | ||
npmPackagesExists(["webpack-addons-foobar"]); | ||
setTimeout(() => { | ||
expect( | ||
mockResolvePackages.mock.calls[ | ||
mockResolvePackages.mock.calls.length - 1 | ||
][0] | ||
).toEqual(["webpack-addons-foobar"]); | ||
done(); | ||
}, 10); | ||
}); | ||
}); |
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