Skip to content

Commit 5780c84

Browse files
authored
Fix Prettier inconsistencies between generated scripts and JS client (#63)
1 parent fd72e33 commit 5780c84

File tree

19 files changed

+165
-151
lines changed

19 files changed

+165
-151
lines changed

.changeset/brown-dragons-kiss.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-solana-program": patch
3+
---
4+
5+
Fix Prettier inconsistencies between generated scripts and JS client

scripts/build.mjs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
#!/usr/bin/env zx
2-
import * as esbuild from "esbuild";
2+
import * as esbuild from 'esbuild';
33

44
await esbuild.build({
55
bundle: true,
6-
entryPoints: ["index.ts"],
7-
external: ["locales/*"],
8-
outfile: "outfile.cjs",
9-
format: "cjs",
10-
platform: "node",
11-
target: "node14",
6+
entryPoints: ['index.ts'],
7+
external: ['locales/*'],
8+
outfile: 'outfile.cjs',
9+
format: 'cjs',
10+
platform: 'node',
11+
target: 'node14',
1212

1313
plugins: [
1414
{
15-
name: "alias",
15+
name: 'alias',
1616
setup({ onResolve, resolve }) {
1717
onResolve(
18-
{ filter: /^prompts$/, namespace: "file" },
18+
{ filter: /^prompts$/, namespace: 'file' },
1919
async ({ importer, resolveDir }) => {
2020
// we can always use non-transpiled code since we support 14.16.0+
21-
const result = await resolve("prompts/lib/index.js", {
21+
const result = await resolve('prompts/lib/index.js', {
2222
importer,
2323
resolveDir,
24-
kind: "import-statement",
24+
kind: 'import-statement',
2525
});
2626
return result;
2727
}

scripts/prepublish.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
3-
import { PROJECTS } from "./utils.mjs";
2+
import 'zx/globals';
3+
import { PROJECTS } from './utils.mjs';
44

55
await $`pnpm snapshot`;
66

7-
const { version } = await fs.readJSON("./package.json");
7+
const { version } = await fs.readJSON('./package.json');
88
const projects = Object.keys(PROJECTS);
99

10-
const rootDirectory = path.resolve(__dirname, "..");
11-
const projectsDirectory = path.resolve(rootDirectory, "projects");
10+
const rootDirectory = path.resolve(__dirname, '..');
11+
const projectsDirectory = path.resolve(rootDirectory, 'projects');
1212

1313
for (const projectName of projects) {
1414
const projectDirectory = path.resolve(projectsDirectory, projectName);
@@ -19,7 +19,7 @@ for (const projectName of projects) {
1919
try {
2020
await $`git commit -m "version ${version} snapshot"`;
2121
} catch (e) {
22-
if (!e.stdout.includes("nothing to commit")) {
22+
if (!e.stdout.includes('nothing to commit')) {
2323
throw e;
2424
}
2525
}

scripts/snapshot.mjs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
3-
import { CLIENTS, PROJECTS, executeStep } from "./utils.mjs";
2+
import 'zx/globals';
3+
import { CLIENTS, PROJECTS, executeStep } from './utils.mjs';
44

55
$.verbose = false;
66

@@ -13,11 +13,11 @@ const projects =
1313
)
1414
: Object.keys(PROJECTS);
1515
const runTests = !!argv.test;
16-
const scaffoldOnly = !!argv["scaffold-only"];
16+
const scaffoldOnly = !!argv['scaffold-only'];
1717

1818
// Resolve paths.
19-
const bin = path.resolve(__dirname, "../outfile.cjs");
20-
const projectsDirectory = path.resolve(__dirname, "../projects/");
19+
const bin = path.resolve(__dirname, '../outfile.cjs');
20+
const projectsDirectory = path.resolve(__dirname, '../projects/');
2121

2222
if (!fs.existsSync(projectsDirectory)) {
2323
fs.mkdirSync(projectsDirectory);
@@ -33,48 +33,48 @@ for (const projectName of projects) {
3333
// Scaffold the project.
3434
const args = [projectName, ...PROJECTS[projectName]];
3535
await executeStep(
36-
"scaffold the project",
37-
() => $`node ${[bin, ...args, "--force", "--default"]}`
36+
'scaffold the project',
37+
() => $`node ${[bin, ...args, '--force', '--default']}`
3838
);
3939

4040
if (scaffoldOnly) continue;
4141

4242
// Go inside the created project.
4343
const projectDirectory = path.resolve(projectsDirectory, projectName);
4444
cd(projectDirectory);
45-
const pkg = await fs.readJSON(path.resolve(projectDirectory, "package.json"));
45+
const pkg = await fs.readJSON(path.resolve(projectDirectory, 'package.json'));
4646

4747
// Install project's dependencies.
48-
await executeStep("install NPM dependencies", async () => {
48+
await executeStep('install NPM dependencies', async () => {
4949
await $`pnpm install`;
5050
});
5151

5252
// Generate IDLs.
53-
if ("generate:idls" in pkg.scripts) {
54-
await executeStep("generate IDLs", async () => {
53+
if ('generate:idls' in pkg.scripts) {
54+
await executeStep('generate IDLs', async () => {
5555
await $`pnpm generate:idls`;
5656
});
5757
}
5858

5959
// Generate clients.
60-
if ("generate:clients" in pkg.scripts) {
61-
await executeStep("generate clients", async () => {
60+
if ('generate:clients' in pkg.scripts) {
61+
await executeStep('generate clients', async () => {
6262
await $`pnpm generate:clients`;
6363
});
6464
}
6565

6666
// Build programs.
67-
if ("programs:build" in pkg.scripts) {
68-
await executeStep("build programs", async () => {
67+
if ('programs:build' in pkg.scripts) {
68+
await executeStep('build programs', async () => {
6969
await $`pnpm programs:build`;
7070
});
7171
}
7272

7373
if (!runTests) continue;
7474

7575
// Test programs.
76-
if ("programs:test" in pkg.scripts) {
77-
await executeStep("test programs", async () => {
76+
if ('programs:test' in pkg.scripts) {
77+
await executeStep('test programs', async () => {
7878
await $`pnpm programs:test`;
7979
});
8080
}
@@ -92,7 +92,7 @@ for (const projectName of projects) {
9292
}
9393

9494
// Add line break between projects.
95-
echo("");
95+
echo('');
9696
}
9797

98-
echo(chalk.green("All projects were created successfully!"));
98+
echo(chalk.green('All projects were created successfully!'));

scripts/utils.mjs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
export const COUNTER_ADDRESS = "CounterProgram111111111111111111111111111111";
2-
export const CLIENTS = ["js", "rust"];
1+
export const COUNTER_ADDRESS = 'CounterProgram111111111111111111111111111111';
2+
export const CLIENTS = ['js', 'rust'];
33
export const PROJECTS = {
4-
"counter-anchor": ["counter", "--anchor", "--address", COUNTER_ADDRESS],
5-
"counter-shank": ["counter", "--shank", "--address", COUNTER_ADDRESS],
4+
'counter-anchor': ['counter', '--anchor', '--address', COUNTER_ADDRESS],
5+
'counter-shank': ['counter', '--shank', '--address', COUNTER_ADDRESS],
66
};
77

88
export async function executeStep(title, fn) {
99
try {
1010
const capitalizedTitle = title.charAt(0).toUpperCase() + title.slice(1);
1111
if (process.env.CI) {
12-
echo(chalk.blue("⠋") + ` ${capitalizedTitle}...`);
12+
echo(chalk.blue('⠋') + ` ${capitalizedTitle}...`);
1313
await fn();
1414
} else {
1515
await spinner(`${capitalizedTitle}...`, fn);
1616
}
17-
echo(chalk.green("✔︎") + ` ${capitalizedTitle}.`);
17+
echo(chalk.green('✔︎') + ` ${capitalizedTitle}.`);
1818
} catch (e) {
19-
echo(chalk.red("✘") + ` Failed to ${title}.\n`);
19+
echo(chalk.red('✘') + ` Failed to ${title}.\n`);
2020
echo(e);
2121
process.exit(1);
2222
}

template/base/.prettierrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"semi": true,
3+
"singleQuote": true,
4+
"trailingComma": "es5",
5+
"useTabs": false,
6+
"tabWidth": 2,
7+
"arrowParens": "always",
8+
"printWidth": 80
9+
}

template/base/scripts/generate-idls.mjs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
3-
import { generateIdl } from "@metaplex-foundation/shank-js";
4-
import { getCargo, getProgramFolders } from "./utils.mjs";
2+
import 'zx/globals';
3+
import { generateIdl } from '@metaplex-foundation/shank-js';
4+
import { getCargo, getProgramFolders } from './utils.mjs';
55

6-
const binaryInstallDir = path.join(__dirname, "..", ".cargo");
6+
const binaryInstallDir = path.join(__dirname, '..', '.cargo');
77

88
getProgramFolders().forEach((folder) => {
99
const cargo = getCargo(folder);
10-
const isShank = Object.keys(cargo.dependencies).includes("shank");
11-
const programDir = path.join(__dirname, "..", folder);
10+
const isShank = Object.keys(cargo.dependencies).includes('shank');
11+
const programDir = path.join(__dirname, '..', folder);
1212

1313
generateIdl({
14-
generator: isShank ? "shank" : "anchor",
15-
programName: cargo.package.name.replace(/-/g, "_"),
16-
programId: cargo.package.metadata.solana["program-id"],
14+
generator: isShank ? 'shank' : 'anchor',
15+
programName: cargo.package.name.replace(/-/g, '_'),
16+
programId: cargo.package.metadata.solana['program-id'],
1717
idlDir: programDir,
18-
idlName: "idl",
18+
idlName: 'idl',
1919
programDir,
2020
binaryInstallDir,
2121
});

template/base/scripts/program/build.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
3-
import { workingDirectory, getProgramFolders } from "../utils.mjs";
2+
import 'zx/globals';
3+
import { workingDirectory, getProgramFolders } from '../utils.mjs';
44

55
// Save external programs binaries to the output directory.
6-
import "./dump.mjs";
6+
import './dump.mjs';
77

88
// Build the programs.
99
for (const folder of getProgramFolders()) {

template/base/scripts/program/clean.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
3-
import { getExternalProgramOutputDir } from "../utils.mjs";
2+
import 'zx/globals';
3+
import { getExternalProgramOutputDir } from '../utils.mjs';
44

55
// Remove the programs output directories.
66
const externalProgramOutput = getExternalProgramOutputDir();

template/base/scripts/program/dump.mjs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
2+
import 'zx/globals';
33
import {
44
getExternalAccountAddresses,
55
getExternalProgramAddresses,
66
getExternalProgramOutputDir,
7-
} from "../utils.mjs";
7+
} from '../utils.mjs';
88

99
// Get input from environment variables.
10-
const rpc = process.env.RPC ?? "https://api.mainnet-beta.solana.com";
10+
const rpc = process.env.RPC ?? 'https://api.mainnet-beta.solana.com';
1111
const outputDir = getExternalProgramOutputDir();
1212
await dump();
1313

@@ -17,8 +17,8 @@ async function dump() {
1717
const programs = getExternalProgramAddresses();
1818
const accounts = getExternalAccountAddresses();
1919
const external = [
20-
...programs.map((program) => [program, "so"]),
21-
...accounts.map((account) => [account, "json"]),
20+
...programs.map((program) => [program, 'so']),
21+
...accounts.map((account) => [account, 'json']),
2222
];
2323

2424
if (external.length === 0) return;
@@ -39,50 +39,50 @@ async function dump() {
3939
return;
4040
}
4141

42-
let sha = "sha256sum";
42+
let sha = 'sha256sum';
4343
let options = [];
44-
let hasShaChecksum = await which("sha256sum", { nothrow: true });
44+
let hasShaChecksum = await which('sha256sum', { nothrow: true });
4545

4646
// We might not have sha256sum on some systems, so we try shasum as well.
4747
if (!hasShaChecksum) {
48-
hasShaChecksum = await which("shasum", { nothrow: true });
48+
hasShaChecksum = await which('shasum', { nothrow: true });
4949

5050
if (hasShaChecksum) {
51-
sha = "shasum";
52-
options = ["-a", "256"];
51+
sha = 'shasum';
52+
options = ['-a', '256'];
5353
}
5454
}
5555

5656
if (hasShaChecksum) {
5757
try {
58-
await copyFromChain(address, extension, "onchain-");
58+
await copyFromChain(address, extension, 'onchain-');
5959
const [onChainHash, localHash] = await Promise.all([
6060
$`${sha} ${options} -b ${outputDir}/onchain-${binary} | cut -d ' ' -f 1`.quiet(),
6161
$`${sha} ${options} -b ${outputDir}/${binary} | cut -d ' ' -f 1`.quiet(),
6262
]);
6363

6464
if (onChainHash.toString() !== localHash.toString()) {
6565
echo(
66-
chalk.yellow("[ WARNING ]"),
66+
chalk.yellow('[ WARNING ]'),
6767
`on-chain and local binaries are different for '${address}'`
6868
);
6969
} else {
7070
echo(
71-
chalk.green("[ SKIPPED ]"),
71+
chalk.green('[ SKIPPED ]'),
7272
`on-chain and local binaries are the same for '${address}'`
7373
);
7474
}
7575

7676
await $`rm ${outputDir}/onchain-${binary}`.quiet();
7777
} catch (error) {
7878
echo(
79-
chalk.yellow("[ WARNING ]"),
79+
chalk.yellow('[ WARNING ]'),
8080
`skipped check for '${address}' (error copying data from '${rpc}')`
8181
);
8282
}
8383
} else {
8484
echo(
85-
chalk.yellow("[ WARNING ]"),
85+
chalk.yellow('[ WARNING ]'),
8686
`skipped check for '${address}' (missing 'sha256sum' command)`
8787
);
8888
}
@@ -91,12 +91,12 @@ async function dump() {
9191
}
9292

9393
/** Helper function to copy external programs or accounts binaries from the chain. */
94-
async function copyFromChain(address, extension, prefix = "") {
94+
async function copyFromChain(address, extension, prefix = '') {
9595
const binary = `${prefix}${address}.${extension}`;
9696
switch (extension) {
97-
case "json":
97+
case 'json':
9898
return $`solana account -u ${rpc} ${address} -o ${outputDir}/${binary} --output json >/dev/null`.quiet();
99-
case "so":
99+
case 'so':
100100
return $`solana program dump -u ${rpc} ${address} ${outputDir}/${binary} >/dev/null`.quiet();
101101
default:
102102
echo(chalk.red(`[ ERROR ] unknown account type for '${binary}'`));

template/base/scripts/program/format.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
3-
import { workingDirectory, getProgramFolders } from "../utils.mjs";
2+
import 'zx/globals';
3+
import { workingDirectory, getProgramFolders } from '../utils.mjs';
44

55
// Format the programs.
66
for (const folder of getProgramFolders()) {

template/base/scripts/program/lint.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env zx
2-
import "zx/globals";
3-
import { workingDirectory, getProgramFolders } from "../utils.mjs";
2+
import 'zx/globals';
3+
import { workingDirectory, getProgramFolders } from '../utils.mjs';
44

55
// Lint the programs using clippy.
66
for (const folder of getProgramFolders()) {

0 commit comments

Comments
 (0)