Skip to content
This repository was archived by the owner on Aug 12, 2021. It is now read-only.

Commit 3cadfd5

Browse files
polunzhpolunzh
andauthored
blocklet:use prompts refinement (#366)
* modify blocklet:use prompts #367 * bump version * add npm package integrity verification when run blocklet:use Also fix the emtpy blocklet directory bug. Related issue: #367 * update CHANGELOG Co-authored-by: polunzh <[email protected]>
1 parent df2a240 commit 3cadfd5

6 files changed

Lines changed: 70 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.0.14 (March 04, 2020)
2+
3+
- add npm package integrity verification when run blocklet:use
4+
- modify blocklet:use prompts
5+
16
## 1.0.13 (February 29, 2020)
27

38
- optimizing install blocket dependencies

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@arcblock/forge-cli",
3-
"version": "1.0.13",
3+
"version": "1.0.14",
44
"description": "a general set of CLI for arcblock Forge framework",
55
"license": "Apache-2.0",
66
"main": "src/index.js",
@@ -118,6 +118,7 @@
118118
"safe-eval": "^0.4.1",
119119
"semver": "^5.6.0",
120120
"shelljs": "^0.8.1",
121+
"ssri": "^8.0.0",
121122
"tar": "^4.4.10",
122123
"update-notifier": "^3.0.1",
123124
"yaml": "^1.3.2"

src/cli/blocklet/use/use.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const promptTargetDirectory = async (checkEmpty = true) => {
3535
const { targetDir } = await inquirer.prompt({
3636
type: 'text',
3737
name: 'targetDir',
38-
message: 'Please input target directory:',
38+
message: 'Where do you want to put the generated project/dApp?',
3939
validate: input => {
4040
if (!input) return 'Target directory should not be empty';
4141

src/core/util.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs');
2+
const fsExtra = require('fs-extra');
23
const os = require('os');
34
const path = require('path');
45
const axios = require('axios');
@@ -14,6 +15,7 @@ const getPort = require('get-port');
1415
const prettyMilliseconds = require('pretty-ms');
1516
const moment = require('moment');
1617
const rc = require('rc');
18+
const ssri = require('ssri');
1719
const util = require('util');
1820
const toLower = require('lodash/toLower');
1921

@@ -245,27 +247,62 @@ function getPackageConfig(filePath) {
245247
return packageJSON;
246248
}
247249

250+
const verifyNpmPackageIntegrity = (content, packageName) => {
251+
const { code, stdout: expectedIntegrity, stderr } = shell.exec(
252+
`npm view ${packageName} dist.integrity`,
253+
{
254+
silent: true,
255+
}
256+
);
257+
258+
if (code !== 0) {
259+
throw new Error(stderr);
260+
}
261+
262+
if (ssri.checkData(content, expectedIntegrity) === false) {
263+
printInfo('expected integrity', expectedIntegrity);
264+
printInfo('actual integrity', ssri.fromData(content));
265+
throw new Error(`${packageName} verify integrity failed`);
266+
}
267+
268+
return true;
269+
};
270+
248271
const downloadPackageFromNPM = async (name, dest, registry = '') => {
249-
fs.mkdirSync(dest, { recursive: true });
272+
printInfo('Downloading package...');
250273
debug('starter directory:', dest);
251274

252-
printInfo('Downloading package...');
253275
let packCommand = `npm pack ${name} --color`;
254-
if (name) {
276+
if (registry) {
255277
packCommand = `${packCommand} --registry=${registry}`;
256278
}
257279

280+
const tmpDir = os.tmpdir();
258281
const { code, stdout, stderr } = shell.exec(packCommand, {
259282
silent: true,
260-
cwd: dest,
283+
cwd: os.tmpdir(),
261284
});
262285

263286
if (code !== 0) {
264287
throw new Error(stderr);
265288
}
266289

267290
const packageName = stdout.trim();
268-
await tar.x({ file: path.join(dest, packageName), C: dest, strip: 1 });
291+
const tarballPath = path.join(tmpDir, packageName);
292+
if (!fs.existsSync(tarballPath)) {
293+
throw new Error(`download ${packageName} failed`);
294+
}
295+
296+
verifyNpmPackageIntegrity(fs.readFileSync(tarballPath), name);
297+
298+
fs.mkdirSync(dest, { recursive: true });
299+
await tar.x({ file: tarballPath, C: dest, strip: 1 });
300+
301+
try {
302+
fsExtra.removeSync(tarballPath);
303+
} catch (error) {
304+
printWarning('remove temp tarball file failed:', tarballPath);
305+
}
269306

270307
return dest;
271308
};

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.13
1+
1.0.14

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4057,6 +4057,13 @@ minipass@^2.6.0, minipass@^2.8.6, minipass@^2.9.0:
40574057
safe-buffer "^5.1.2"
40584058
yallist "^3.0.0"
40594059

4060+
minipass@^3.1.1:
4061+
version "3.1.1"
4062+
resolved "https://registry.yarnpkg.com/minipass/-/minipass-3.1.1.tgz#7607ce778472a185ad6d89082aa2070f79cedcd5"
4063+
integrity sha512-UFqVihv6PQgwj8/yTGvl9kPz7xIAY+R5z6XYjRInD3Gk3qx6QGSD6zEcpeG4Dy/lQnv1J6zv8ejV90hyYIKf3w==
4064+
dependencies:
4065+
yallist "^4.0.0"
4066+
40604067
minizlib@^1.2.1:
40614068
version "1.3.3"
40624069
resolved "https://registry.yarnpkg.com/minizlib/-/minizlib-1.3.3.tgz#2290de96818a34c29551c8a8d301216bd65a861d"
@@ -5628,6 +5635,13 @@ sshpk@^1.7.0:
56285635
safer-buffer "^2.0.2"
56295636
tweetnacl "~0.14.0"
56305637

5638+
ssri@^8.0.0:
5639+
version "8.0.0"
5640+
resolved "https://registry.yarnpkg.com/ssri/-/ssri-8.0.0.tgz#79ca74e21f8ceaeddfcb4b90143c458b8d988808"
5641+
integrity sha512-aq/pz989nxVYwn16Tsbj1TqFpD5LLrQxHf5zaHuieFV+R0Bbr4y8qUsOA45hXT/N4/9UNXTarBjnjVmjSOVaAA==
5642+
dependencies:
5643+
minipass "^3.1.1"
5644+
56315645
stack-utils@^1.0.1:
56325646
version "1.0.2"
56335647
resolved "https://registry.yarnpkg.com/stack-utils/-/stack-utils-1.0.2.tgz#33eba3897788558bebfc2db059dc158ec36cebb8"
@@ -6363,6 +6377,11 @@ yallist@^3.0.0, yallist@^3.0.3:
63636377
resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.1.1.tgz#dbb7daf9bfd8bac9ab45ebf602b8cbad0d5d08fd"
63646378
integrity sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==
63656379

6380+
yallist@^4.0.0:
6381+
version "4.0.0"
6382+
resolved "https://registry.yarnpkg.com/yallist/-/yallist-4.0.0.tgz#9bb92790d9c0effec63be73519e11a35019a3a72"
6383+
integrity sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==
6384+
63666385
yaml@^1.3.2:
63676386
version "1.7.2"
63686387
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.7.2.tgz#f26aabf738590ab61efaca502358e48dc9f348b2"

0 commit comments

Comments
 (0)