Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6b7cf6f

Browse files
committed
init
0 parents  commit6b7cf6f

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+
constfs=require("fs");
2+
constutil=require("util");
3+
const{ join}=require("path");
4+
5+
constreadFile=util.promisify(fs.readFile);
6+
constreaddir=util.promisify(fs.readdir);
7+
8+
constgetPackageJson=async(dir=process.cwd())=>{
9+
// load package.json file
10+
constpathToPackageJson=join(dir,"package.json");
11+
constpackageJson=awaitreadFile(pathToPackageJson,"utf8").catch(
12+
console.error
13+
);
14+
if(!packageJson){
15+
thrownewError("Missing root package.json");
16+
}
17+
// parse as JSON
18+
constjson=JSON.parse(packageJson);
19+
if(!json){
20+
thrownewError("The package.json content looks invalid");
21+
}
22+
returnjson;
23+
};
24+
25+
exports.getPackageJson=getPackageJson;
26+
27+
constversionMatch=(current,expected)=>{
28+
letcurrentSemver=current;
29+
if(["~","^"].includes(current[0])){
30+
currentSemver=current.substring(1);
31+
}
32+
returncurrentSemver===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+
constisModuleInstalled=async({ name, type, version})=>{
43+
// 1. load package.json file
44+
constjson=awaitgetPackageJson();
45+
46+
// 2. verify package lists dependency by type
47+
letinstallCommand;
48+
lethasDependency;
49+
letcurrentVersion;
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+
thrownewError("Peer dependencies unsupported");
64+
default:
65+
thrownewError("Unsupported packaged type");
66+
}
67+
68+
if(!hasDependency){
69+
thrownewError(`Run "npm install${installCommand}${name}"`);
70+
}
71+
72+
// 3. if version, check dependency version
73+
if(version&&!versionMatch(currentVersion,version)){
74+
thrownewError(
75+
`Dependency${name} version${currentVersion} does not match expected${version}`
76+
);
77+
}
78+
79+
// 4. verify node_module installed
80+
constpathToNodeModule=join(process.cwd(),"node_modules",name);
81+
consthasNodeModules=awaitreaddir(pathToNodeModule).catch(()=>{
82+
thrownewError('Missing node_modules. Run "npm install"');
83+
});
84+
if(!hasNodeModules){
85+
thrownewError('Missing node_modules. Run "npm install"');
86+
}
87+
88+
// 5. if version, has installed node_module version
89+
if(version){
90+
constnodeModulePackageJson=awaitgetPackageJson(pathToNodeModule);
91+
if(!versionMatch(nodeModulePackageJson.version,version)){
92+
thrownewError(
93+
`Dependency${name} version${version} is not yet installed. Run "npm install"`
94+
);
95+
}
96+
}
97+
98+
returntrue;
99+
};
100+
101+
exports.isModuleInstalled=isModuleInstalled;
102+
103+
// created because assert.doesNotThrow not working predictably with async fns
104+
constdoesNotThrow=async(fn)=>{
105+
letresult=true;
106+
try{
107+
awaitfn();
108+
}catch(error){
109+
console.error(error);
110+
result=false;
111+
}
112+
returnresult;
113+
};
114+
115+
exports.doesNotThrow=doesNotThrow;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp