Skip to content

Commit a7c9b53

Browse files
wip
1 parent c9cb0b7 commit a7c9b53

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

src/cli.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import chalk from "chalk";
88
import yargs from "yargs";
99

1010
import databaseCommand from "./commands/database/database.mjs";
11-
import localCommand from "./commands/local.mjs";
1211
import initCommand from "./commands/init.mjs";
12+
import localCommand from "./commands/local.mjs";
1313
import loginCommand from "./commands/login.mjs";
1414
import queryCommand from "./commands/query.mjs";
1515
import schemaCommand from "./commands/schema/schema.mjs";

src/commands/database/list.mjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ import { faunaToCommandError } from "../../lib/fauna.mjs";
66
import { FaunaAccountClient } from "../../lib/fauna-account-client.mjs";
77
import { colorize, Format } from "../../lib/formatting/colorize.mjs";
88

9-
async function listDatabasesWithAccountAPI(argv) {
9+
export async function listDatabasesWithAccountAPI(argv) {
1010
const { pageSize, database } = argv;
1111
const accountClient = new FaunaAccountClient();
1212
const response = await accountClient.listDatabases({
1313
pageSize,
1414
path: database,
1515
});
1616

17-
return response.results.map(({ path, name }) => ({ path, name }));
17+
// eslint-disable-next-line camelcase
18+
return response.results.map(({ path, name, region_group }) => ({
19+
path,
20+
name,
21+
regionGroup: region_group, // eslint-disable-line camelcase
22+
}));
1823
}
1924

2025
async function listDatabasesWithSecret(argv) {

src/commands/init.mjs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import { getSecret } from "../lib/fauna-client.mjs";
1515
import { reformatFSL } from "../lib/schema.mjs";
1616
import { listDatabasesWithAccountAPI } from "./database/list.mjs";
1717

18-
// TODO: handle error/exit case cleanly
19-
2018
async function doInit(argv) {
2119
const logger = container.resolve("logger");
2220
const getters = [getDatabaseRunnable, getProjectRunnable, getKeyRunnable];
@@ -65,11 +63,11 @@ async function getDatabaseRunnable(argv /*, priorChoices*/) {
6563

6664
buildCredentials({ ...argv, user: "default", database: "us-std" });
6765
const dbs = (await listDatabasesWithAccountAPI(argv))
68-
.map((db) => `${db.region_group}/${db.name}`)
66+
.map((db) => `${db.regionGroup}/${db.name}`)
6967
.map((dbName) => ({ value: dbName }));
7068

7169
runnable.choices.createNewDb = await inquirer.select({
72-
message: "Select a database to create a project for",
70+
message: "Select a database to create an FSL directory for",
7371
choices: [
7472
...dbs,
7573
{
@@ -81,6 +79,7 @@ async function getDatabaseRunnable(argv /*, priorChoices*/) {
8179

8280
if (runnable.choices.createNewDb !== "new") {
8381
runnable.choices.dbName = runnable.choices.createNewDb.split("/")[1];
82+
runnable.choices.regionGroup = runnable.choices.createNewDb.split("/")[0];
8483
return runnable;
8584
}
8685

@@ -249,26 +248,29 @@ async function getProjectRunnable(argv, priorChoices) {
249248
if (!shouldCreateProjectDirectory) return runnable;
250249

251250
runnable.choices.dirName = await inquirer.input({
252-
message: `FSL files are stored in a project directory and are specific to the database "${priorChoices.dbName}". What would you like to name this project directory? In the next step, you will choose where to put the directory.`,
251+
message: `FSL files are stored in a directory and are specific to the database "${priorChoices.dbName}". What would you like to name this directory? In the next step, you will choose where to put the directory.`,
253252
default: priorChoices.dbName,
254253
});
255254

256255
runnable.choices.dirPath = await fileSelector({
257-
message: "Where would you like the project directory to be created?",
256+
message: "Where would you like the FSL directory to be created?",
258257
type: "directory",
259258
});
260259

261-
runnable.runner = async ({
262-
choices: { createNewDb, demoData, dirPath, dirName, dbName, regionGroup },
263-
}) => {
260+
runnable.runner = async ({ choices }) => {
261+
const { createNewDb, demoData, dirPath, dirName, dbName, regionGroup } =
262+
choices;
264263
const fs = container.resolve("fs");
265264
const fsp = container.resolve("fsp");
266265
const path = await import("path");
267-
logger.stdout(
268-
`Creating project directory at ${path.join(dirPath, dirName)}`,
269-
);
266+
logger.stdout(`Creating FSL directory at ${path.join(dirPath, dirName)}`);
270267

271-
if (!createNewDb) {
268+
buildCredentials({
269+
...argv,
270+
user: "default",
271+
database: `${regionGroup}/${dbName}`,
272+
});
273+
if (createNewDb !== "new") {
272274
// existing db? fetch the schema
273275
// TODO: this has huge overlap with schema pull - should refactor so it's only in one place
274276
fs.mkdirSync(path.join(dirPath, dirName), { recursive: true });
@@ -281,6 +283,13 @@ async function getProjectRunnable(argv, priorChoices) {
281283
secret,
282284
});
283285

286+
const { version } = await makeFaunaRequest({
287+
argv,
288+
path: "/schema/1/staged/status",
289+
method: "GET",
290+
secret,
291+
});
292+
284293
// sort for consistent order (it's nice for tests)
285294
const filenames = filesResponse.files
286295
.map((file) => file.filename)
@@ -290,10 +299,12 @@ async function getProjectRunnable(argv, priorChoices) {
290299
const getAllSchemaFileContents = container.resolve(
291300
"getAllSchemaFileContents",
292301
);
293-
const contents = await getAllSchemaFileContents(filenames, {
294-
...argv,
295-
secret,
296-
});
302+
const contents = await getAllSchemaFileContents(
303+
filenames,
304+
"active",
305+
version,
306+
argv,
307+
);
297308

298309
// don't start writing files until we've successfully fetched all the remote schema files
299310
const promises = [];
@@ -321,11 +332,6 @@ async function getProjectRunnable(argv, priorChoices) {
321332
staged: "false",
322333
});
323334

324-
buildCredentials({
325-
...argv,
326-
user: "default",
327-
database: `${regionGroup}/${dbName}`,
328-
});
329335
await makeFaunaRequest({
330336
argv,
331337
path: "/schema/1/update",
@@ -347,9 +353,7 @@ async function getProjectRunnable(argv, priorChoices) {
347353
),
348354
]);
349355
}
350-
logger.stdout(
351-
`Created project directory at ${path.join(dirPath, dirName)}`,
352-
);
356+
logger.stdout(`Created FSL directory at ${path.join(dirPath, dirName)}`);
353357
};
354358

355359
return runnable;

0 commit comments

Comments
 (0)