-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add populate option when create api project
Signed-off-by: Brian Nguyen <[email protected]>
- Loading branch information
Showing
6 changed files
with
134 additions
and
6 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
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,47 @@ | ||
import fs from "fs/promises"; | ||
import sinon from "sinon"; | ||
import { expect } from "chai"; | ||
import Logger from "../utils/logger.js"; | ||
import addDependency from "../utils/addDependency.js"; | ||
|
||
describe("The addDependency function", () => { | ||
const mockPackageJson = { | ||
name: "test", | ||
dependencies: { | ||
"plugin-1": "1.0.0" | ||
} | ||
}; | ||
it("should update the package.json with the added dependency", async () => { | ||
const expectedPath = "testProject/package.json"; | ||
const expectedPackageJson = { ...mockPackageJson }; | ||
expectedPackageJson.dependencies.testPackage = "^1.0.0"; | ||
|
||
const loggerInfoStub = sinon.stub(Logger, "info"); | ||
const writeFileStub = sinon.stub(fs, "writeFile"); | ||
const readFileStub = sinon.stub(fs, "readFile").resolves(JSON.stringify(mockPackageJson)); | ||
|
||
const result = await addDependency("testProject", "testPackage", { version: "1.0.0" }); | ||
|
||
expect(loggerInfoStub.calledWith("Add the testPackage@^1.0.0 package to project")).eq(true); | ||
expect(readFileStub.calledWith(expectedPath)).eq(true); | ||
expect(writeFileStub.calledWith(expectedPath, JSON.stringify(expectedPackageJson, null, "\t"))).eq(true); | ||
expect(result).eq(true); | ||
}); | ||
|
||
it("should update the package.json with the added dependency and don't include the version option", async () => { | ||
const expectedPath = "testProject/package.json"; | ||
const expectedPackageJson = { ...mockPackageJson }; | ||
expectedPackageJson.dependencies.testPackage = "latest"; | ||
|
||
const loggerInfoStub = sinon.stub(Logger, "info"); | ||
const writeFileStub = sinon.stub(fs, "writeFile"); | ||
const readFileStub = sinon.stub(fs, "readFile").resolves(JSON.stringify(mockPackageJson)); | ||
|
||
const result = await addDependency("testProject", "testPackage"); | ||
|
||
expect(loggerInfoStub.calledWith("Add the testPackage@latest package to project")).eq(true); | ||
expect(readFileStub.calledWith(expectedPath)).eq(true); | ||
expect(writeFileStub.calledWith(expectedPath, JSON.stringify(expectedPackageJson, null, "\t"))).eq(true); | ||
expect(result).eq(true); | ||
}); | ||
}); |
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,23 @@ | ||
import fs from "fs/promises"; | ||
import Logger from "./logger.js"; | ||
|
||
/** | ||
* @summary Add the dependencies for the package.json file | ||
* @param {String} projectName projectName name of the project to add | ||
* @param {String} packageName name of the package to add | ||
* @param {Object} options the package name | ||
* @returns {Promise<boolean>} true for success | ||
*/ | ||
export default async function addDependency(projectName, packageName, { version = "latest", rule = "^" } = {}) { | ||
const versionString = version === "latest" ? version : `${rule}${version}`; | ||
|
||
Logger.info(`Add the ${packageName}@${versionString} package to project`); | ||
|
||
const packageJsonPath = `${projectName}/package.json`; | ||
const packageJson = await fs.readFile(packageJsonPath); | ||
const packageJsonData = JSON.parse(packageJson); | ||
packageJsonData.dependencies[packageName] = versionString; | ||
await fs.writeFile(packageJsonPath, JSON.stringify(packageJsonData, null, "\t")); | ||
|
||
return true; | ||
} |
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 wget from "./wget.js"; | ||
|
||
/** | ||
* @summary Get latest npm package version | ||
* @param {String} packageName name of the npm package | ||
* @returns {Promise<string|null>} the version string of the npm package | ||
*/ | ||
export default async function getLatestNpmVersion(packageName) { | ||
const packageDesc = await wget(`https://registry.npmjs.org/${packageName}`); | ||
const packageDescData = JSON.parse(packageDesc); | ||
return packageDescData["dist-tags"].latest; | ||
} |