Added command "flipper-pkg checksum"

Summary: Changelog: Added command `flipper-pkg checksum` for computing the total checksum of all the files included into plugin package.

Reviewed By: passy

Differential Revision: D22255125

fbshipit-source-id: a4f91370b4ab16ea4ce4a60e29f9d20fdd5f4331
This commit is contained in:
Anton Nikolaev
2020-06-26 03:22:32 -07:00
committed by Facebook GitHub Bot
parent 56c9435bd5
commit 419691da97
7 changed files with 150 additions and 12 deletions

View File

@@ -0,0 +1,37 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {Command} from '@oclif/command';
import {args} from '@oclif/parser';
import path from 'path';
import computePackageChecksum from '../utils/computePackageChecksum';
export default class Lint extends Command {
public static description =
'computes the total checksum of all the package files';
public static examples = [`$ flipper-pkg checksum path/to/plugin`];
public static args: args.IArg[] = [
{
name: 'directory',
required: false,
default: '.',
description:
'Path to plugin package directory. Defaults to the current working directory.',
},
];
public async run() {
const {args} = this.parse(Lint);
const inputDirectory: string = path.resolve(process.cwd(), args.directory);
const checksum = await computePackageChecksum(inputDirectory);
console.log(checksum);
}
}

View File

@@ -0,0 +1,50 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import packlist from 'npm-packlist';
import path from 'path';
import crypto from 'crypto';
import fs from 'fs-extra';
export default async function computePackageChecksum(
dir: string,
): Promise<string> {
const hash = crypto.createHash('sha1');
hash.setEncoding('hex');
const files = (await packlist({path: dir})).sort();
for (const file of files) {
// add hash of relative file path
hash.write(process.platform === 'win32' ? file.replace(/\\/g, '/') : file);
const filePath = path.resolve(dir, file);
if (file === 'package.json') {
// add hash of package.json with version set to "0.0.0" to avoid changing hash when only version changed
const packageJson = await fs.readJson(filePath);
if (packageJson.version) {
packageJson.version = '0.0.0';
}
hash.write(JSON.stringify(packageJson));
} else {
// add hash of file content
const stream = fs.createReadStream(filePath);
try {
stream.pipe(hash, {end: false});
await new Promise((resolve, reject) => {
stream.once('end', resolve);
stream.once('error', reject);
});
} finally {
stream.close();
}
}
}
hash.end();
return hash.read();
}