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

chore: move chain packages to a subfolder #703

Merged
merged 3 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,21 @@ Runs all tests:

- `npm test` (or the shorthand, `npm t`)

## To create a new chain/flavor

- `npm run create <name> --location chains`

This will create a new folder at `src/chains/<name>` where `<name>` should be the flavor name (e.g. `ethereum`), which
you then can [create packages under](#to-create-a-new-package).

## To create a new package

- `npm run create <name> --location <location>`
- `npm run create <name> --location <location> [--folder <folder>]`

This will create a new package with Ganache defaults at `src/<location>/<name>`.

If you provide the optional `--folder` option, the package will be created at `src/<location>/<folder>`.

## To add a module to a package:

- `npx lerna add <module>[@version] -E [--dev] [--peer] --scope=<package>`
Expand Down
2 changes: 1 addition & 1 deletion completions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ _npmScriptsCompletions() {
while [[ "$#" -gt 0 ]]; do
case $1 in
-l | --location)
type_list=$(ls src)
type_list=$(cd src && find * chains/* -maxdepth 0 -type d && cd ../)
shift
;;
- | --l | --lo | --loc | --loca | --locat | --locati | --locatio)
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"packages": ["src/packages/*", "src/chains/*"],
"packages": ["src/packages/*", "src/chains/*/*"],
"version": "independent"
}
43 changes: 0 additions & 43 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"create": "ts-node ./scripts/create",
"docs.build": "lerna run docs.build && ts-node ./scripts/build-docs/",
"docs.preview": "lerna run docs.preview",
"postinstall": "lerna bootstrap && ts-node ./scripts/postinstall",
"postinstall": "lerna bootstrap && ts-node ./scripts/link-ts-references.ts && ts-node ./scripts/postinstall",
"reinstall": "npm run clean && npm install",
"test": "lerna exec -- npm run test",
"tsc": "lerna exec -- npm run tsc",
Expand All @@ -33,13 +33,13 @@
"cross-env": "7.0.2",
"fs-extra": "9.0.1",
"git-user-name": "2.0.0",
"glob": "7.1.6",
"husky": "4.3.0",
"into-stream": "6.0.0",
"lerna": "3.22.1",
"marked": "1.2.7",
"mocha": "8.2.0",
"monaco-editor": "0.21.2",
"npm-package-arg": "8.1.0",
"nyc": "15.1.0",
"prettier": "2.1.2",
"pretty-quick": "3.1.0",
Expand All @@ -48,6 +48,7 @@
"ts-transformer-inline-file": "0.1.1",
"ttypescript": "1.5.12",
"typescript": "4.1.1-rc",
"validate-npm-package-name": "3.0.0",
"yargs": "16.1.0"
},
"license": "MIT",
Expand Down
138 changes: 95 additions & 43 deletions scripts/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import chalk from "chalk";
import yargs from "yargs";
import prettier from "prettier";
import camelCase from "camelcase";
import npa from "npm-package-arg";
import npmValiddate from "validate-npm-package-name";
import userName from "git-user-name";
import { join, resolve } from "path";
import { sep, join, resolve } from "path";
import { highlight } from "cli-highlight";
import { mkdir, mkdirSync, writeFile } from "fs-extra";
import {
Expand All @@ -15,6 +15,24 @@ import {
readFileSync as readFile
} from "fs";

const COMMAND_NAME = "create";

const getArgv = () => {
const npmConfigArgv = process.env["npm_config_argv"];
if (npmConfigArgv) {
// handle `npm run create name --location chains`
// convert original npm args into a command
// create <name> --location <location> [--folder <folder>]
return JSON.parse(npmConfigArgv).original.slice(1);
} else {
// handle `ts-node ./scripts/create.ts name --location chains`

const args = [...process.argv].slice(2);
args.unshift(COMMAND_NAME);
return args;
}
};

const isDir = (s: string) => lstat(s).isDirectory();
const getDirectories = (s: string) => readDir(s).filter(n => isDir(join(s, n)));

Expand All @@ -24,39 +42,42 @@ const COLORS = {
FgRed: "\x1b[31m"
};

const scopes = getDirectories(join(__dirname, "../src"));
const argv = yargs
.command(
`$0 <name> --location`,
`Create a new package in the given location with the provided name.`,
yargs => {
return yargs
.usage(
chalk`{hex("#e4a663").bold Create a new package in the given location with the provided name.}\n\n` +
chalk`{bold Usage}\n {bold $} {dim <}name{dim >} {dim --}location {dim <}${scopes.join(
chalk.dim(" | ")
)}{dim >}`
)
.positional("<name>", {
describe: ` The name of the new package`,
type: "string",
demandOption: true
})
.alias("name", "<name>")
.option("location", {
alias: "l",
default: "packages",
describe: `The location for the new package.`,
choices: scopes,
type: "string",
demandOption: true
});
}
)
let locations = getDirectories(join(__dirname, "../src"));
const chainLocations = getDirectories(join(__dirname, "../src/chains")).map(
d => `chains/${d}`
);
locations = locations.concat(chainLocations);

const argv = yargs(getArgv())
.command(`${COMMAND_NAME} <name>`, "", yargs => {
return yargs
.usage(
chalk`{hex("#e4a663").bold Create a new package in the given {dim <}location{dim >} with the provided {dim <}name{dim >}.}\n\n` +
chalk`{bold Usage}\n {bold $} ${COMMAND_NAME} {dim <}name{dim >} {dim --}location {dim <}location{dim >} {dim [--folder <folder>]}`
)
.positional("name", {
// the spaces here are a hack to make this command description line up with the others in the help output
describe: ` The name for the new package.`,
type: "string",
demandOption: true
})
.option("location", {
alias: "l",
describe: `The location for the new package.`,
choices: locations,
demandOption: true
})
.option("folder", {
alias: "f",
describe: chalk`Optional override for the folder name for the package instead of using {dim <}name{dim >}.`,
type: "string"
});
})
.demandCommand()
.version(false)
.help(false)
.updateStrings({
// a little hack just to join the "Positionals" section with the "Options" section, for brevity
"Positionals:": chalk.bold("Options"),
"Options:": ` `,
"Not enough non-option arguments: got %s, need at least %s": {
Expand All @@ -77,17 +98,42 @@ const argv = yargs
process.stdout.write(`${COLORS.Reset}`);

(async function () {
let name = argv.name;
let location = argv.location;
const { name, location } = argv;
const folderName = argv.folder || name;

const nameValidation = npmValiddate(name);
if (!nameValidation.validForNewPackages) {
throw new Error(
`the name "${name}" is not a valid npm package name:\n${nameValidation.errors}`
);
}

// determines how many `../` are needed for package contents
const numDirectoriesAwayFromRoot = 3 + location.split(sep).length;
const relativePathToRoot = "../".repeat(numDirectoriesAwayFromRoot);
const isNewChain = location === "chains";

const workspaceDir = join(__dirname, "../");
const dir = join(workspaceDir, "src", location, folderName);

if (isNewChain) {
mkdirSync(dir);

const fullLocation = join(location, folderName);
console.log(
chalk`{green success} {magenta create} New chain folder {bgBlack ${name} } created at ./src/${fullLocation}.`
);
console.log("");
console.log(
chalk` Add packages to this chain by running: {bgBlack {bold npm run create {dim <}name{dim >} {dim --}location ${fullLocation}}}`
);
return;
}
try {
const workspaceDir = join(__dirname, "../");
const LICENSE = readFile(join(workspaceDir, "LICENSE"), "utf-8");

const prettierConfig = await prettier.resolveConfig(process.cwd());

name = npa(name).name;

const packageName = `@ganache/${name}`;
let packageAuthor = userName();
const version = "0.1.0";
Expand All @@ -97,10 +143,10 @@ process.stdout.write(`${COLORS.Reset}`);
version,
description: "",
author: packageAuthor || require("../package.json").author,
homepage: `https://github.com/trufflesuite/ganache-core/tree/develop/src/${location}/${name}#readme`,
homepage: `https://github.com/trufflesuite/ganache-core/tree/develop/src/${location}/${folderName}#readme`,
license: "MIT",
main: "lib/index.js",
types: "src/index.ts",
types: "lib/index.d.ts",
source: "index.ts",
directories: {
lib: "lib",
Expand All @@ -110,7 +156,7 @@ process.stdout.write(`${COLORS.Reset}`);
repository: {
type: "git",
url: "https://github.com/trufflesuite/ganache-core.git",
directory: `src/${location}/${name}`
directory: `src/${location}/${folderName}`
},
scripts: {
tsc: "ttsc",
Expand Down Expand Up @@ -139,7 +185,7 @@ process.stdout.write(`${COLORS.Reset}`);
};

const tsConfig = {
extends: "../../../tsconfig.json",
extends: `${relativePathToRoot}tsconfig.json`,
compilerOptions: {
outDir: "lib"
},
Expand All @@ -164,14 +210,16 @@ describe("${packageName}", () => {
}
`;

const dir = join(workspaceDir, "src", location, name);
const tests = join(dir, "tests");
const src = join(dir, "src");

function initSrc() {
return writeFile(
join(src, "index.ts"),
prettier.format(indexFile, { ...prettierConfig, parser: "typescript" })
prettier.format(indexFile, {
...prettierConfig,
parser: "typescript"
})
);
}

Expand Down Expand Up @@ -263,7 +311,11 @@ typedoc.json
);

console.log(
chalk`{green success} {magenta create} New package {bgBlack ${name} } created at ./src/packages/${name}.`
chalk`{green success} {magenta create} New package {bgBlack ${name} } created at .${sep}${join(
"src",
location,
folderName
)}.`
);
console.log("");
console.log(
Expand Down
Loading