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

Commit2d6ce80

Browse files
committed
2.1 install moment
1 parent95f7224 commit2d6ce80

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

‎test/packagejson.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const{ getPackageJson}=require("./utils");
1+
const{ getPackageJson, isModuleInstalled, doesNotThrow}=require("./utils");
22
constassert=require("assert");
33

44
describe("package.json",()=>{
@@ -54,4 +54,21 @@ describe("package.json", () => {
5454
'should have a "version" value that is a string'
5555
);
5656
});
57+
// 2.1
58+
it('should have "dependencies"',()=>{
59+
assert.ok(json.dependencies,'"dependencies" is missing');
60+
assert.equal(
61+
typeofjson.dependencies,
62+
"object",
63+
'should have a "dependencies" value that is an object'
64+
);
65+
});
66+
it('should have installed "moment"',async()=>{
67+
assert.ok(
68+
awaitdoesNotThrow(
69+
()=>isModuleInstalled({name:"moment",type:"dependency"}),
70+
'"moment" not installed'
71+
)
72+
);
73+
});
5774
});

‎test/utils.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const{
2-
promises:{ readFile}
2+
promises:{ readFile, readdir}
33
}=require("fs");
44
const{ join}=require("path");
55

@@ -21,3 +21,66 @@ const getPackageJson = async () => {
2121
};
2222

2323
exports.getPackageJson=getPackageJson;
24+
25+
/**
26+
* isModuleInstalled
27+
*@param { name, type} params
28+
* "name" - the name of the dependency
29+
* "type" - "dependency", "devDependency", "peerDependency"
30+
*@returns boolean
31+
*/
32+
constisModuleInstalled=async({ name, type})=>{
33+
// 1. load package.json file
34+
constjson=awaitgetPackageJson();
35+
36+
// 2. verify package lists dependency by type
37+
letinstallCommand;
38+
lethasDependency;
39+
40+
switch(type){
41+
case"dependency":
42+
installCommand="--save";
43+
hasDependency=!!json.dependencies&&json.dependencies[name];
44+
break;
45+
case"devDependency":
46+
installCommand="--save-dev";
47+
hasDependency=!!json.devDependencies&&json.devDependencies[name];
48+
break;
49+
case"peerDependency":
50+
thrownewError("Peer dependencies unsupported");
51+
default:
52+
thrownewError("Unsupported packaged type");
53+
}
54+
55+
if(!hasDependency){
56+
thrownewError(`Run "npm install${installCommand}${name}"`);
57+
}
58+
59+
// 3. verify node_module installed
60+
constpathToNodeModule=join(process.cwd(),"node_modules",name);
61+
consthasNodeModules=awaitreaddir(pathToNodeModule).catch(()=>{
62+
thrownewError('Missing node_modules. Run "npm install"');
63+
});
64+
if(!hasNodeModules){
65+
thrownewError('Missing node_modules. Run "npm install"');
66+
}
67+
68+
// 4. is installed
69+
returntrue;
70+
};
71+
72+
exports.isModuleInstalled=isModuleInstalled;
73+
74+
// created because assert.doesNotThrow not working predictably with async fns
75+
constdoesNotThrow=asyncfn=>{
76+
letresult=true;
77+
try{
78+
awaitfn();
79+
}catch(error){
80+
console.error(error);
81+
result=false;
82+
}
83+
returnresult;
84+
};
85+
86+
exports.doesNotThrow=doesNotThrow;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp