-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·191 lines (177 loc) · 6.07 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env satsuma
// Keep this one at the top to suppress warnings about fetch API
import 'suppress-experimental-warnings';
import * as fs from 'fs';
import * as yargs from 'yargs';
import v1Cli from './versions/v1';
import {run} from "./shared/helpers/cli";
import {CliVersion, InitArgs, RunServerArgs, SupportedVersions, WithSubgraphData} from "./shared/types";
import {checkForNpmUpdates, getCurrentPackage} from "./shared/helpers/npm";
import { exec } from 'child_process';
import * as util from 'util';
import ora from "ora";
import spinners from "cli-spinners";
import {sleepAwait} from "sleep-await";
const versions: Record<SupportedVersions, CliVersion> = {
[SupportedVersions.v1]: v1Cli,
};
interface SatsumaJson {
version: string;
}
const checkVersion = (version: string): version is SupportedVersions => {
return Object.values(SupportedVersions).includes(
version as SupportedVersions
);
};
// Read the default version from the satsuma.json file
const DEFAULT_VERSION = (() => {
try {
const {version} = JSON.parse(
fs.readFileSync("./.satsuma.json", "utf8")
) as SatsumaJson;
return version;
} catch (err) {
return null;
}
})();
const NEWEST_VERSION = "v1";
const cliOptions = yargs
.option("cli-version", {
alias: "v",
describe: "Version of the script to run",
type: "string",
choices: ["v1"],
default: DEFAULT_VERSION || NEWEST_VERSION,
})
.option("deploy-key", {
alias: "k",
describe: "Your Satsuma deploy key",
type: "string",
demandOption: false,
})
.option("subgraph-name", {
describe: "The name of the subgraph",
type: "string",
demandOption: false,
})
.option("version-name", {
describe: "The name of the version you want to deploy to",
type: "string",
demandOption: false,
})
.option("debug", {
describe: "Enable debug mode",
type: "boolean",
demandOption: false,
})
.command({
command: "init",
describe: "Initialize the satsuma project",
handler: () => {},
})
.command({
command: "deploy",
describe: "Deploy to Satsuma.xyz 🍊",
handler: () => {},
})
.command({
command: "validate",
describe: "Validate your custom queries.",
handler: () => {},
})
.command({
command: "local",
describe: "Run a local graphql server.",
handler: () => {},
})
.command({
command: "codegen",
describe: "Generate the graphql schema & types.",
handler: () => {},
})
.command({
command: "ignore",
describe: "",
handler: () => {},
})
.command({
command: "selfupdate",
describe: "Update the Satsuma CLI",
handler: () => {},
})
.parseSync();
if (require.main === module) {
run(async () => {
const { currentVersion } = getCurrentPackage();
console.log(`🍊 Satsuma CLI version ${currentVersion}. Project Version: ${cliOptions.cliVersion}.`);
const cmd = cliOptions._[0];
if (cmd !== "selfupdate") {
await checkForNpmUpdates();
}
switch (cmd) {
case "init":
if (checkVersion(cliOptions.cliVersion)) {
await versions[cliOptions.cliVersion].init(
cliOptions as unknown as InitArgs
);
} else {
throw new Error(`Unsupported version: ${cliOptions.cliVersion}`);
}
break;
case "deploy":
if (checkVersion(cliOptions.cliVersion)) {
await versions[cliOptions.cliVersion].deploy(
cliOptions as unknown as WithSubgraphData
);
} else {
throw new Error(`Unsupported version: ${cliOptions.cliVersion}`);
}
break;
case "validate":
if (checkVersion(cliOptions.cliVersion)) {
await versions[cliOptions.cliVersion].validate(
cliOptions as unknown as WithSubgraphData
);
} else {
throw new Error(`Unsupported version: ${cliOptions.cliVersion}`);
}
break;
case "local":
if (checkVersion(cliOptions.cliVersion)) {
await versions[cliOptions.cliVersion].local(
cliOptions as unknown as RunServerArgs
);
} else {
throw new Error(`Unsupported version: ${cliOptions.cliVersion}`);
}
break;
case 'codegen':
if (checkVersion(cliOptions.cliVersion)) {
await versions[cliOptions.cliVersion].codegen(cliOptions as unknown as WithSubgraphData)
} else {
throw new Error(`Unsupported version: ${cliOptions.cliVersion}`);
}
break;
case 'selfupdate':
const onCurrentVersion = await checkForNpmUpdates(true);
if (onCurrentVersion){
ora().succeed('Satsuma CLI is already up to date!');
return;
}
let { currentVersion } = getCurrentPackage();
const spinner = ora({text: `Updating Satsuma CLI from ${currentVersion}...`, spinner: "moon"}).start();
try {
await util.promisify(exec)('npx --yes clear-npx-cache; npx --yes @satsuma/cli ignore', {shell: '/bin/bash'});
currentVersion = getCurrentPackage().currentVersion;
spinner.succeed(`Updated Satsuma CLI to ${currentVersion}!`);
} catch {
spinner.fail(`Error!`);
}
break;
default:
ora(`Unsupported command: ${cmd}`).fail();
return;
}
});
}
export {};