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

Commit6bfa2e3

Browse files
committed
INIT - setup
0 parents  commit6bfa2e3

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

‎.gitignore

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

‎.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

‎coderoad/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name":"coderoad-fcc-learn-npm",
3+
"repository": {
4+
"type":"git",
5+
"url":"https://github.com/coderoad/fcc-learn-npm"
6+
},
7+
"scripts": {
8+
"test":"mocha",
9+
"programmatic-test":"mocha --reporter=mocha-tap-reporter"
10+
},
11+
"dependencies": {
12+
"mocha":"^7.0.1",
13+
"mocha-tap-reporter":"^0.1.3"
14+
}
15+
}

‎coderoad/test/utils.js

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp