From 419691da97eab0ba2b15d9bace48fb2f490379e4 Mon Sep 17 00:00:00 2001 From: Anton Nikolaev Date: Fri, 26 Jun 2020 03:22:32 -0700 Subject: [PATCH] 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 --- desktop/package.json | 1 + desktop/pkg-lib/README.md | 3 +- desktop/pkg/README.md | 41 +++++++++++---- desktop/pkg/package.json | 1 + desktop/pkg/src/commands/checksum.ts | 37 ++++++++++++++ .../pkg/src/utils/computePackageChecksum.ts | 50 +++++++++++++++++++ desktop/yarn.lock | 29 ++++++++++- 7 files changed, 150 insertions(+), 12 deletions(-) create mode 100644 desktop/pkg/src/commands/checksum.ts create mode 100644 desktop/pkg/src/utils/computePackageChecksum.ts diff --git a/desktop/package.json b/desktop/package.json index 75d126e7c..8c6bbd8c0 100644 --- a/desktop/package.json +++ b/desktop/package.json @@ -128,6 +128,7 @@ "@types/mkdirp": "^1.0.0", "@types/mock-fs": "^4.10.0", "@types/node": "^13.13.0", + "@types/npm-packlist": "^1.1.1", "@types/promise-retry": "^1.1.3", "@types/react": "^16.9.17", "@types/react-color": "^3.0.1", diff --git a/desktop/pkg-lib/README.md b/desktop/pkg-lib/README.md index c40d9b27a..f248b24e1 100644 --- a/desktop/pkg-lib/README.md +++ b/desktop/pkg-lib/README.md @@ -1,4 +1,3 @@ # flipper-pkg -`flipper-pkg-lib` is a **work-in-progress** library for bundling and publishing -Flipper plugins. +`flipper-pkg-lib` is a library for bundling and publishing Flipper plugins. diff --git a/desktop/pkg/README.md b/desktop/pkg/README.md index a93c2e28f..988b3d34a 100644 --- a/desktop/pkg/README.md +++ b/desktop/pkg/README.md @@ -1,7 +1,6 @@ # flipper-pkg -`flipper-pkg` is a **work-in-progress** tool for bundling and publishing -Flipper plugins. +`flipper-pkg` is a tool for bundling and publishing Flipper plugins. * [flipper-pkg](#flipper-pkg) @@ -15,7 +14,7 @@ $ npm install -g flipper-pkg $ flipper-pkg COMMAND running command... $ flipper-pkg (-v|--version|version) -flipper-pkg/0.40.2 darwin-x64 node-v12.15.0 +flipper-pkg/0.48.0 darwin-x64 node-v12.15.0 $ flipper-pkg --help [COMMAND] USAGE $ flipper-pkg COMMAND @@ -25,6 +24,7 @@ USAGE # Commands * [`flipper-pkg bundle [DIRECTORY]`](#flipper-pkg-bundle-directory) +* [`flipper-pkg checksum [DIRECTORY]`](#flipper-pkg-checksum-directory) * [`flipper-pkg help [COMMAND]`](#flipper-pkg-help-command) * [`flipper-pkg init [DIRECTORY]`](#flipper-pkg-init-directory) * [`flipper-pkg lint [DIRECTORY]`](#flipper-pkg-lint-directory) @@ -42,11 +42,32 @@ USAGE ARGUMENTS DIRECTORY [default: .] Path to plugin package directory for bundling. Defaults to the current working directory. +OPTIONS + --production Force env.NODE_ENV=production, enable minification and disable producing source maps. + --watch Watch for plugin source code and bundle it after every change. + EXAMPLE $ flipper-pkg bundle path/to/plugin ``` -_See code: [src/commands/bundle.ts](https://github.com/facebook/flipper/blob/v0.40.2/src/commands/bundle.ts)_ +_See code: [src/commands/bundle.ts](https://github.com/facebook/flipper/blob/v0.48.0/src/commands/bundle.ts)_ + +## `flipper-pkg checksum [DIRECTORY]` + +computes the total checksum of all the package files + +``` +USAGE + $ flipper-pkg checksum [DIRECTORY] + +ARGUMENTS + DIRECTORY [default: .] Path to plugin package directory. Defaults to the current working directory. + +EXAMPLE + $ flipper-pkg checksum path/to/plugin +``` + +_See code: [src/commands/checksum.ts](https://github.com/facebook/flipper/blob/v0.48.0/src/commands/checksum.ts)_ ## `flipper-pkg help [COMMAND]` @@ -63,7 +84,7 @@ OPTIONS --all see all commands in CLI ``` -_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v3.0.0/src/commands/help.ts)_ +_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v3.0.1/src/commands/help.ts)_ ## `flipper-pkg init [DIRECTORY]` @@ -81,7 +102,7 @@ EXAMPLE $ flipper-pkg init path/to/plugin ``` -_See code: [src/commands/init.ts](https://github.com/facebook/flipper/blob/v0.40.2/src/commands/init.ts)_ +_See code: [src/commands/init.ts](https://github.com/facebook/flipper/blob/v0.48.0/src/commands/init.ts)_ ## `flipper-pkg lint [DIRECTORY]` @@ -98,7 +119,7 @@ EXAMPLE $ flipper-pkg lint path/to/plugin ``` -_See code: [src/commands/lint.ts](https://github.com/facebook/flipper/blob/v0.40.2/src/commands/lint.ts)_ +_See code: [src/commands/lint.ts](https://github.com/facebook/flipper/blob/v0.48.0/src/commands/lint.ts)_ ## `flipper-pkg migrate [DIRECTORY]` @@ -119,7 +140,7 @@ EXAMPLE $ flipper-pkg migrate path/to/plugin ``` -_See code: [src/commands/migrate.ts](https://github.com/facebook/flipper/blob/v0.40.2/src/commands/migrate.ts)_ +_See code: [src/commands/migrate.ts](https://github.com/facebook/flipper/blob/v0.48.0/src/commands/migrate.ts)_ ## `flipper-pkg pack [DIRECTORY]` @@ -136,11 +157,13 @@ OPTIONS -o, --output=output [default: .] Where to output the package, file or directory. Defaults to the current working directory. + --production Force env.NODE_ENV=production, enable minification and disable producing source maps. + EXAMPLE $ flipper-pkg pack path/to/plugin ``` -_See code: [src/commands/pack.ts](https://github.com/facebook/flipper/blob/v0.40.2/src/commands/pack.ts)_ +_See code: [src/commands/pack.ts](https://github.com/facebook/flipper/blob/v0.48.0/src/commands/pack.ts)_ diff --git a/desktop/pkg/package.json b/desktop/pkg/package.json index a2642437d..ddf4ae18c 100644 --- a/desktop/pkg/package.json +++ b/desktop/pkg/package.json @@ -25,6 +25,7 @@ "fs-extra": "^8.1.0", "inquirer": "^7.1.0", "lodash": "^4.17.15", + "npm-packlist": "^2.1.2", "recursive-readdir": "^2.2.2" }, "devDependencies": { diff --git a/desktop/pkg/src/commands/checksum.ts b/desktop/pkg/src/commands/checksum.ts new file mode 100644 index 000000000..d2ca93b0a --- /dev/null +++ b/desktop/pkg/src/commands/checksum.ts @@ -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); + } +} diff --git a/desktop/pkg/src/utils/computePackageChecksum.ts b/desktop/pkg/src/utils/computePackageChecksum.ts new file mode 100644 index 000000000..ff811898c --- /dev/null +++ b/desktop/pkg/src/utils/computePackageChecksum.ts @@ -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 { + 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(); +} diff --git a/desktop/yarn.lock b/desktop/yarn.lock index 3e7094d1f..d7a3c3f62 100644 --- a/desktop/yarn.lock +++ b/desktop/yarn.lock @@ -2073,6 +2073,11 @@ resolved "https://registry.yarnpkg.com/@types/normalize-package-data/-/normalize-package-data-2.4.0.tgz#e486d0d97396d79beedd0a6e33f4534ff6b4973e" integrity sha512-f5j5b/Gf71L+dbqxIpQ4Z2WlmI/mPJ0fOkGGmFgtb6sAu97EPczzbS3/tJKxmcYDj55OX6ssqwDAWOHIYDRDGA== +"@types/npm-packlist@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@types/npm-packlist/-/npm-packlist-1.1.1.tgz#b8bdb1e2910aa693ab9bf8339361b09c8d512ecd" + integrity sha512-+0ZRUpPOs4Mvvwj/pftWb14fnPN/yS6nOp6HZFyIMDuUmyPtKXcO4/SPhyRGR6dUCAn1B3hHJozD/UCrU+Mmew== + "@types/pako@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@types/pako/-/pako-1.0.1.tgz#33b237f3c9aff44d0f82fe63acffa4a365ef4a61" @@ -6323,7 +6328,7 @@ ieee754@^1.1.4: resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.13.tgz#ec168558e95aa181fd87d37f55c32bbcb6708b84" integrity sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg== -ignore-walk@^3.0.1: +ignore-walk@^3.0.1, ignore-walk@^3.0.3: version "3.0.3" resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.3.tgz#017e2447184bfeade7c238e4aefdd1e8f95b1e37" integrity sha512-m7o6xuOaT1aqheYHKf8W6J5pYH85ZI9w077erOzLje3JsB1gkafkAhHHY19dqjulgIZHFm32Cp5uNZgcQqdJKw== @@ -8960,6 +8965,13 @@ npm-bundled@^1.0.1: resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.6.tgz#e7ba9aadcef962bb61248f91721cd932b3fe6bdd" integrity sha512-8/JCaftHwbd//k6y2rEWp6k1wxVfpFzB6t1p825+cUb7Ym2XQfhwIC5KwhrvzZRJu+LtDE585zVaS32+CGtf0g== +npm-bundled@^1.1.1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.1.1.tgz#1edd570865a94cdb1bc8220775e29466c9fb234b" + integrity sha512-gqkfgGePhTpAEgUsGEgcq1rqPXA+tv/aVBlgEzfXwA1yiUJF7xtEt3CtVwOjNYQOVknDk0F20w58Fnm3EtG0fA== + dependencies: + npm-normalize-package-bin "^1.0.1" + npm-conf@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/npm-conf/-/npm-conf-1.1.3.tgz#256cc47bd0e218c259c4e9550bf413bc2192aff9" @@ -8973,6 +8985,11 @@ npm-install-package@~2.1.0: resolved "https://registry.yarnpkg.com/npm-install-package/-/npm-install-package-2.1.0.tgz#d7efe3cfcd7ab00614b896ea53119dc9ab259125" integrity sha1-1+/jz816sAYUuJbqUxGdyaslkSU= +npm-normalize-package-bin@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/npm-normalize-package-bin/-/npm-normalize-package-bin-1.0.1.tgz#6e79a41f23fd235c0623218228da7d9c23b8f6e2" + integrity sha512-EPfafl6JL5/rU+ot6P3gRSCpPDW5VmIzX959Ob1+ySFUuuYHWHekXpwdUZcKP5C+DS4GEtdJluwBjnsNDl+fSA== + npm-packlist@^1.1.6: version "1.4.6" resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-1.4.6.tgz#53ba3ed11f8523079f1457376dd379ee4ea42ff4" @@ -8981,6 +8998,16 @@ npm-packlist@^1.1.6: ignore-walk "^3.0.1" npm-bundled "^1.0.1" +npm-packlist@^2.1.2: + version "2.1.2" + resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-2.1.2.tgz#a3045b52aefc37e7a5e86a55e6ca8cb1e909e25a" + integrity sha512-eByPaP+wsKai0BJX5pmb58d3mfR0zUATcnyuvSxIudTEn+swCPFLxh7srCmqB4hr7i9V24/DPjjq5b2qUtbgXQ== + dependencies: + glob "^7.1.6" + ignore-walk "^3.0.3" + npm-bundled "^1.1.1" + npm-normalize-package-bin "^1.0.1" + npm-run-path@^2.0.0: version "2.0.2" resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f"