Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

chore: throw if name arg in create script includes scope #3108

Merged

Conversation

tenthirtyone
Copy link
Contributor

Description

Very minor change to the create.ts script that checks for a scope in the name arg. This is implicitly documented in the README but the validate-npm-package-name will allow for @scope/name to get through the validation check. When this happens, the creation script will bomb out with a partially created package.

Steps to Validate

  1. From develop branch
    npm run create roflcopter --location chains

Expected: New 'roflcopter' chain is created in chains folder.

  1. Run npm run create @ganache/roflcopter-options --location chains/roflcopter --folder options

Expected: Error: Name @ganache/roflcopter-options includes scope and is an invalid name.
Actual: options folder will be created in the roflcopter folder, test will not be created, package.json name will be @ganache/@ganache/roflcopter-options

Error Message:

» npm run create @ganache/roflcopter-options --location chains/roflcopter --folder options                                                                                                                                                                                                                                 130 ↵ hayek@rothbard

> root@ create /home/hayek/proj/ganache
> cd scripts && ts-node create "@ganache/roflcopter-options" "chains/roflcopter" "options"

About to write to /home/hayek/proj/ganache/src/chains/roflcopter/options/package.json

SyntaxError: Declaration or statement expected. (2:1)
  1 | import assert from "assert";
> 2 | import @ganache/roflcopterOptions from "../src/";
    | ^
  3 |
  4 | describe("@ganache/@ganache/roflcopter-options", () => {
  5 |   it("needs tests");
    at r (/home/hayek/proj/ganache/node_modules/prettier/parser-typescript.js:1:557)
    at yx (/home/hayek/proj/ganache/node_modules/prettier/parser-typescript.js:13:3304376)
    at Object.parse (/home/hayek/proj/ganache/node_modules/prettier/parser-typescript.js:13:3304817)
    at Object.parse$d [as parse] (/home/hayek/proj/ganache/node_modules/prettier/index.js:12975:19)
    at coreFormat (/home/hayek/proj/ganache/node_modules/prettier/index.js:14525:16)
    at formatWithCursor$1 (/home/hayek/proj/ganache/node_modules/prettier/index.js:14765:14)
    at /home/hayek/proj/ganache/node_modules/prettier/index.js:60959:12
    at Object.format (/home/hayek/proj/ganache/node_modules/prettier/index.js:60979:12)
    at initTests (/home/hayek/proj/ganache/scripts/create.ts:287:18)
    at async Promise.all (index 2) {
  loc: { start: { line: 2, column: 1 } },
  codeFrame: '\x1B[0m \x1B[90m 1 |\x1B[39m \x1B[36mimport\x1B[39m assert \x1B[36mfrom\x1B[39m \x1B[32m"assert"\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n' +
    '\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 2 |\x1B[39m \x1B[36mimport\x1B[39m \x1B[33m@\x1B[39mganache\x1B[33m/\x1B[39mroflcopterOptions \x1B[36mfrom\x1B[39m \x1B[32m"../src/"\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m   |\x1B[39m \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 3 |\x1B[39m\x1B[0m\n' +
    '\x1B[0m \x1B[90m 4 |\x1B[39m describe(\x1B[32m"@ganache/@ganache/roflcopter-options"\x1B[39m\x1B[33m,\x1B[39m () \x1B[33m=>\x1B[39m {\x1B[0m\n' +
    '\x1B[0m \x1B[90m 5 |\x1B[39m   it(\x1B[32m"needs tests"\x1B[39m)\x1B[33m;\x1B[39m\x1B[0m'
}

fail create New package  @ganache/roflcopter-options  not created. See error above.

Steps to Test

  1. From this branch
    npm run create roflcopter --location chains

Expected: New 'roflcopter' chain is created in chains folder.

  1. Run npm run create @ganache/roflcopter-options --location chains/roflcopter --folder options

Actual:

  1. No package scaffolding is created
  2. Error Message:
npm run create @ganache/roflcopter-options --location chains/roflcopter --folder options                                                                                                                                                                                                               hayek@rothbard

> root@ create /home/hayek/proj/ganache
> cd scripts && ts-node create "@ganache/roflcopter-options" "chains/roflcopter" "options"

(node:21887) UnhandledPromiseRejectionWarning: Error: the name "@ganache/roflcopter-options" is not a valid npm package name:
**Do not include scope in name**
    at /home/hayek/proj/ganache/scripts/create.ts:113:11
    at Object.<anonymous> (/home/hayek/proj/ganache/scripts/create.ts:355:3)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module.m._compile (/home/hayek/proj/ganache/node_modules/ts-node/src/index.ts:1371:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Object.require.extensions.<computed> [as .ts] (/home/hayek/proj/ganache/node_modules/ts-node/src/index.ts:1374:12)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at main (/home/hayek/proj/ganache/node_modules/ts-node/src/bin.ts:331:12)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:21887) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:21887) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Notes

The validation package will catch names similar to @<name> because the @ is not url-friendly. However, it will validate the @<scope>/<name> names because it is a valid npm package. This is a very narrow bug.

Existing validation will catch:
@<name>
<name>/<name>
<name>@<name>

This change catches:

@<name>/<name>

See: Examples

Copy link
Member

@davidmurdoch davidmurdoch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

Copy link
Contributor

@MicaiahReid MicaiahReid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and reviewed, looks good!

@davidmurdoch davidmurdoch changed the title Fix: throw if name arg in create script includes scope chore: throw if name arg in create script includes scope May 26, 2022
@davidmurdoch davidmurdoch merged commit a130a0c into trufflesuite:develop May 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants