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

Commit

Permalink
fix create script
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmurdoch committed Aug 4, 2023
1 parent e303d16 commit 002e341
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
12 changes: 2 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
- `git clone git@github.com:trufflesuite/ganache.git`
- `cd ganache`
- `npm install` (use npm v6)
- On Linux and macOS: run `source completions.sh` to enable autocomplete for npm scripts.

## Solving node-gyp issues

Expand Down Expand Up @@ -97,20 +96,13 @@ To pass options to the cli you must separate the args with `--`, e.g.:

- `npm start -- --chain.chainId 1 --wallet.totalAccounts 5`

## 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> [--folder <folder>]`

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

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

## To add a module to a package:

Expand Down
48 changes: 13 additions & 35 deletions scripts/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { TruffleColors } from "../src/packages/colors";
import { TruffleColors } from "../packages/colors";
import { sep, join, resolve } from "path";
import { highlight } from "cli-highlight";
import { mkdir, mkdirSync, writeFile } from "fs-extra";
import yargs from "yargs";
import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import {
lstatSync as lstat,
readdirSync as readDir,
readFileSync as readFile
} from "fs";

Expand All @@ -22,21 +21,19 @@ const COMMAND_NAME = "create";
const getArgv = () => {
const npmConfigArgv = process.env["npm_config_argv"];
if (npmConfigArgv) {
// handle `npm run create name --location chains`
// handle `npm run create name --location ethereum`
// 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`
// handle `ts-node ./scripts/create.ts name --location packages/ethereum`

const args = [...process.argv].slice(2);
const args = hideBin(process.argv);
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)));
const nameIncludesScope = (s: string) => s.indexOf("@") === 0;

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

let locations = getDirectories(join(__dirname, "../src"));
const chainLocations = getDirectories(join(__dirname, "../src/chains")).map(
d => `chains/${d}`
);
locations = locations.concat(chainLocations);
const locations = ["packages", "packages/ethereum"];

const argv = yargs(getArgv())
.command(`${COMMAND_NAME} <name>`, "", yargs => {
Expand Down Expand Up @@ -116,26 +109,12 @@ process.stdout.write(`${COLORS.Reset}`);
}

// determines how many `../` are needed for package contents
const numDirectoriesAwayFromSrc = 1 + location.split(sep).length;
const relativePathToSrc = "../".repeat(numDirectoriesAwayFromSrc);
const isNewChain = location === "chains";
const numDirectoriesAwayFromPackages = 1 + location.split(sep).length;
const relativePathToPackages = "../".repeat(numDirectoriesAwayFromPackages);

const workspaceDir = join(__dirname, "../");
const dir = join(workspaceDir, "src", location, folderName);
const dir = join(workspaceDir, 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 LICENSE = readFile(join(workspaceDir, "LICENSE"), "utf-8");

Expand All @@ -155,7 +134,7 @@ process.stdout.write(`${COLORS.Reset}`);
version,
description: "",
author: packageAuthor || rootPackageJson.author,
homepage: `https://github.com/trufflesuite/ganache/tree/develop/src/${location}/${folderName}#readme`,
homepage: `https://github.com/trufflesuite/ganache/tree/develop/${location}/${folderName}#readme`,
license: "MIT",
main: "lib/index.js",
typings: "typings",
Expand All @@ -168,7 +147,7 @@ process.stdout.write(`${COLORS.Reset}`);
repository: {
type: "git",
url: "https://github.com/trufflesuite/ganache.git",
directory: `src/${location}/${folderName}`
directory: `${location}/${folderName}`
},
scripts: {
tsc: "tsc --build",
Expand Down Expand Up @@ -205,7 +184,7 @@ process.stdout.write(`${COLORS.Reset}`);
};

const tsConfig = {
extends: `${relativePathToSrc}tsconfig-base.json`,
extends: `${relativePathToPackages}tsconfig-base.json`,
compilerOptions: {
outDir: "lib",
declarationDir: "typings"
Expand Down Expand Up @@ -336,7 +315,6 @@ describe("${packageName}", () => {

console.log(
chalk`{green success} {magenta create} New package {bgBlack ${name} } created at .${sep}${join(
"src",
location,
folderName
)}.`
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true
},
"files": [],
"references": [
{
Expand Down

0 comments on commit 002e341

Please sign in to comment.