Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6b7cf6f

Browse files
committed
init
0 parents  commit 6b7cf6f

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# NPM
2+
node_modules
3+
package-lock.json
4+
5+
# Tutorial
6+
CODEROAD.md

.vscode/extensions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": ["dbaeumer.vscode-eslint"]
5+
}

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"type": "node",
6+
"request": "launch",
7+
"name": "Run Tests",
8+
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
9+
"args": ["--reporter=spec", "--colors", "--bail", "--timeout", "9999999"],
10+
"console": "internalConsole",
11+
"internalConsoleOptions": "openOnSessionStart"
12+
}
13+
]
14+
}

.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll": true
5+
}
6+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Backend Challenges: package.json

test/utils.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
const fs = require("fs");
2+
const util = require("util");
3+
const { join } = require("path");
4+
5+
const readFile = util.promisify(fs.readFile);
6+
const readdir = util.promisify(fs.readdir);
7+
8+
const getPackageJson = async (dir = process.cwd()) => {
9+
// load package.json file
10+
const pathToPackageJson = join(dir, "package.json");
11+
const packageJson = await readFile(pathToPackageJson, "utf8").catch(
12+
console.error
13+
);
14+
if (!packageJson) {
15+
throw new Error("Missing root package.json");
16+
}
17+
// parse as JSON
18+
const json = JSON.parse(packageJson);
19+
if (!json) {
20+
throw new Error("The package.json content looks invalid");
21+
}
22+
return json;
23+
};
24+
25+
exports.getPackageJson = getPackageJson;
26+
27+
const versionMatch = (current, expected) => {
28+
let currentSemver = current;
29+
if (["~", "^"].includes(current[0])) {
30+
currentSemver = current.substring(1);
31+
}
32+
return currentSemver === expected;
33+
};
34+
35+
/**
36+
* isModuleInstalled
37+
* @param { name, type } params
38+
* "name" - the name of the dependency
39+
* "type" - "dependency", "devDependency", "peerDependency"
40+
* @returns boolean
41+
*/
42+
const isModuleInstalled = async ({ name, type, version }) => {
43+
// 1. load package.json file
44+
const json = await getPackageJson();
45+
46+
// 2. verify package lists dependency by type
47+
let installCommand;
48+
let hasDependency;
49+
let currentVersion;
50+
51+
switch (type) {
52+
case "dependency":
53+
installCommand = "--save";
54+
hasDependency = !!json.dependencies && json.dependencies[name];
55+
currentVersion = json.dependencies[name];
56+
break;
57+
case "devDependency":
58+
installCommand = "--save-dev";
59+
hasDependency = !!json.devDependencies && json.devDependencies[name];
60+
currentVersion = json.devDependencies[name];
61+
break;
62+
case "peerDependency":
63+
throw new Error("Peer dependencies unsupported");
64+
default:
65+
throw new Error("Unsupported packaged type");
66+
}
67+
68+
if (!hasDependency) {
69+
throw new Error(`Run "npm install ${installCommand} ${name}"`);
70+
}
71+
72+
// 3. if version, check dependency version
73+
if (version && !versionMatch(currentVersion, version)) {
74+
throw new Error(
75+
`Dependency ${name} version ${currentVersion} does not match expected ${version}`
76+
);
77+
}
78+
79+
// 4. verify node_module installed
80+
const pathToNodeModule = join(process.cwd(), "node_modules", name);
81+
const hasNodeModules = await readdir(pathToNodeModule).catch(() => {
82+
throw new Error('Missing node_modules. Run "npm install"');
83+
});
84+
if (!hasNodeModules) {
85+
throw new Error('Missing node_modules. Run "npm install"');
86+
}
87+
88+
// 5. if version, has installed node_module version
89+
if (version) {
90+
const nodeModulePackageJson = await getPackageJson(pathToNodeModule);
91+
if (!versionMatch(nodeModulePackageJson.version, version)) {
92+
throw new Error(
93+
`Dependency ${name} version ${version} is not yet installed. Run "npm install"`
94+
);
95+
}
96+
}
97+
98+
return true;
99+
};
100+
101+
exports.isModuleInstalled = isModuleInstalled;
102+
103+
// created because assert.doesNotThrow not working predictably with async fns
104+
const doesNotThrow = async (fn) => {
105+
let result = true;
106+
try {
107+
await fn();
108+
} catch (error) {
109+
console.error(error);
110+
result = false;
111+
}
112+
return result;
113+
};
114+
115+
exports.doesNotThrow = doesNotThrow;

0 commit comments

Comments
 (0)