Skip to content
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
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- [ ] csv-to-pg should use inqurirer
- [ ] get this PR from launchql-gen https://github.com/launchql/launchql-gen/pull/19
- [ ] Docker in workflow needs extensions directories

- [ ] LaunchQL class for the project structures — control files, modules, paths, etc.


Good next steps:
Expand Down
5 changes: 5 additions & 0 deletions __fixtures__/sqitch/broken/launchql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"packages": [
"packages/*"
]
}
5 changes: 5 additions & 0 deletions __fixtures__/sqitch/launchql/launchql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"packages": [
"packages/*"
]
}
5 changes: 5 additions & 0 deletions __fixtures__/sqitch/publish/launchql.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"packages": [
"packages/*"
]
}
Empty file.
6 changes: 4 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@
"ts-node": "^10.9.2"
},
"dependencies": {
"@launchql/migrate": "^2.0.0",
"@launchql/explorer": "^2.0.0",
"@launchql/migrate": "^2.0.0",
"@launchql/server": "^2.0.0",
"@launchql/templatizer": "^2.0.0",
"@launchql/types": "^2.0.0",
"chalk": "^4.1.0",
"deepmerge": "^4.3.1",
"inquirerer": "^1.9.1",
"inquirerer": "^2.0.2",
"js-yaml": "^4.1.0",
"minimist": "^1.2.8",
"mkdirp": "^3.0.1",
"shelljs": "^0.9.2"
}
}
20 changes: 20 additions & 0 deletions packages/cli/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import server from './commands/server';
import explorer from './commands/explorer';
import verify from './commands/verify';
import revert from './commands/revert';
import init from './commands/init';
import extension from './commands/extension';

export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer, options: CLIOptions) => {
if (argv.version || argv.v) {
Expand All @@ -24,6 +26,17 @@ export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer,
process.exit(0);
}

await prompter.prompt(argv, [
{
type: 'text',
name: 'cwd',
message: 'Working directory',
required: false,
default: process.cwd(),
useDefault: true
}
]);

switch (command) {
case 'deploy':
await deploy(newArgv, prompter, options);
Expand All @@ -34,12 +47,19 @@ export const commands = async (argv: Partial<ParsedArgs>, prompter: Inquirerer,
case 'revert':
await revert(newArgv, prompter, options);
break;
case 'init':
await init(newArgv, prompter, options);
break;
case 'server':
await server(newArgv, prompter, options);
break;
case 'explorer':
await explorer(newArgv, prompter, options);
break;
case 'extension':
await extension(newArgv, prompter, options);
break;
break;
default:
console.error(`Unknown command: ${command}`);
console.log(usageText);
Expand Down
21 changes: 6 additions & 15 deletions packages/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@ export default async (
_options: CLIOptions
) => {
const questions: Question[] = [
// @ts-ignore
{
type: 'text',
name: 'dir',
message: chalk.cyan('Working directory'),
required: false,
default: false,
useDefault: true
},
{
type: 'text',
name: 'database',
Expand All @@ -34,16 +25,16 @@ export default async (
}
];

let { database, yes, recursive, createdb, dir } = await prompter.prompt(argv, questions);
let { database, yes, recursive, createdb, cwd } = await prompter.prompt(argv, questions);

if (!yes) {
console.log(chalk.gray('Operation cancelled.'));
return;
}

if (!dir) {
dir = process.cwd();
console.log(chalk.gray(`Using current directory: ${dir}`));
if (!cwd) {
cwd = process.cwd();
console.log(chalk.gray(`Using current directory: ${cwd}`));
}

if (createdb) {
Expand All @@ -52,7 +43,7 @@ export default async (
}

if (recursive) {
const modules = await listModules(dir);
const modules = await listModules(cwd);
const mods = Object.keys(modules);

if (!mods.length) {
Expand All @@ -72,7 +63,7 @@ export default async (
]);

console.log(chalk.green(`Deploying project ${chalk.bold(project)} to database ${chalk.bold(database)}...`));
await deploy(project, database, dir);
await deploy(project, database, cwd);
console.log(chalk.green('Deployment complete.'));
} else {
console.log(chalk.green(`Running ${chalk.bold(`sqitch deploy db:pg:${database}`)}...`));
Expand Down
40 changes: 40 additions & 0 deletions packages/cli/src/commands/extension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { CLIOptions, Inquirerer, OptionValue, Question } from 'inquirerer';
import { ParsedArgs } from 'minimist';
import { LaunchQLProject } from '@launchql/migrate';

export default async (
argv: Partial<ParsedArgs>,
prompter: Inquirerer,
_options: CLIOptions
) => {
const { cwd = process.cwd() } = argv;

const project = new LaunchQLProject(cwd);
await project.init();

if (!project.isInModule()) {
throw new Error('You must run this command inside a LaunchQL module.');
}

const info = project.getModuleInfo();
const installed = project.getRequiredModules();
const available = project.getAvailableModules();
const filtered = available.filter(name => name !== info.extname);

const questions: Question[] = [
{
name: 'modules',
message: 'Which modules does this one depend on?',
type: 'checkbox',
options: filtered,
default: installed
}
];

const answers = await prompter.prompt(argv, questions);
const selected = (answers.modules as OptionValue[])
.filter(opt => opt.selected)
.map(opt => opt.name);

project.setModuleDependencies(selected);
};
23 changes: 23 additions & 0 deletions packages/cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CLIOptions, Inquirerer } from 'inquirerer';
import runWorkspaceSetup from './workspace';
import runModuleSetup from './module';

export default async (
argv: Partial<Record<string, any>>,
prompter: Inquirerer,
_options: CLIOptions
) => {
return handlePromptFlow(argv, prompter);
};

async function handlePromptFlow(argv: Partial<Record<string, any>>, prompter: Inquirerer) {
const { workspace } = argv;

switch (workspace) {
case true:
return runWorkspaceSetup(argv, prompter);
case false:
default:
return runModuleSetup(argv, prompter);
}
}
155 changes: 155 additions & 0 deletions packages/cli/src/commands/init/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { Inquirerer, Question } from 'inquirerer';
import chalk from 'chalk';
import * as mkdirp from 'mkdirp';
import path from 'path';
import fs, { writeFileSync } from 'fs';
import glob from 'glob';
import {
writeRenderedTemplates,
moduleTemplate
} from '@launchql/templatizer';
import { getAvailableExtensions, getExtensionInfo, getWorkspacePath, listModules, makePlan, sluggify, writeExtensionControlFile, writeExtensionMakefile } from '@launchql/migrate';
import { exec } from 'shelljs';

function isInsideAllowedDirs(cwd: string, allowedDirs: string[]): boolean {
const resolvedCwd = path.resolve(cwd);
return allowedDirs.some(dir => resolvedCwd.startsWith(dir));
}

function loadAllowedDirs(workspacePath: string): string[] {
const configPath = path.join(workspacePath, 'launchql.json');
if (!fs.existsSync(configPath)) {
throw new Error(`Missing launchql.json at workspace root: ${configPath}`);
}

const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
const globs: string[] = config?.packages ?? [];

const dirs = globs.flatMap(pattern => glob.sync(path.join(workspacePath, pattern)));
return dirs.map(dir => path.resolve(dir));
}

export default async function runModuleSetup(argv: Partial<Record<string, any>>, prompter: Inquirerer) {
const username = exec('git config --global user.name', { silent: true }).trim();
const email = exec('git config --global user.email', { silent: true }).trim();
const { cwd = process.cwd() } = argv;

const workspacePath = await getWorkspacePath(cwd);
const isAtWorkspaceRoot = path.resolve(workspacePath) === path.resolve(cwd);

if (!isAtWorkspaceRoot) {
const allowedDirs = loadAllowedDirs(workspacePath);
if (!isInsideAllowedDirs(cwd, allowedDirs)) {
console.error(chalk.red(`Error: You must be inside one of the workspace packages: ${allowedDirs.join(', ')}`));
process.exit(1);
}
}


const moduleMap = await listModules(workspacePath);
const availExtensions = await getAvailableExtensions(moduleMap)


const moduleQuestions: Question[] = [
// {
// name: 'USERFULLNAME',
// message: 'Enter author full name',
// required: true,
// default: username,
// useDefault: true,
// type: 'text',
// },
// {
// name: 'USEREMAIL',
// message: 'Enter author email',
// required: true,
// default: email,
// useDefault: true,
// type: 'text',
// },
{
name: 'MODULENAME',
message: 'Enter the module name',
required: true,
type: 'text',
},
// {
// name: 'REPONAME',
// message: 'Enter the repository name',
// required: true,
// type: 'text',
// },
// {
// name: 'USERNAME',
// message: 'Enter your GitHub username',
// required: true,
// type: 'text',
// },
// {
// name: 'ACCESS',
// message: 'Module access?',
// required: true,
// type: 'autocomplete',
// options: ['public', 'restricted'],
// },
{
name: 'extensions',
message: 'which extensions?',
options: availExtensions,
type: 'checkbox',
// default: ['plpgsql'],
// default: [{
// name: 'plpgsql',
// value: 'plpgsql'
// }],
required: true
},
];

const answers = await prompter.prompt(argv, moduleQuestions);
const modName = sluggify(answers.MODULENAME);
let targetPath: string;

if (isAtWorkspaceRoot) {
const packagesDir = path.join(cwd, 'packages');
mkdirp.sync(packagesDir);
targetPath = path.join(packagesDir, modName);
mkdirp.sync(targetPath);
console.log(chalk.green(`Created module in workspace packages/: ${targetPath}`));
} else {
targetPath = path.join(cwd, modName);
mkdirp.sync(targetPath);
console.log(chalk.green(`Created module: ${targetPath}`));
}

const cur = process.cwd();
process.chdir(targetPath);

writeRenderedTemplates(moduleTemplate, targetPath, { ...argv, ...answers });

const cmd = ['sqitch', 'init', modName, '--engine', 'pg'].join(' ');
exec(cmd.trim());

const plan = `%syntax-version=1.0.0
%project=${modName}
%uri=${modName}`;

writeFileSync(`${targetPath}/sqitch.plan`, plan);

const info = await getExtensionInfo(targetPath);

await writeExtensionMakefile(
info.Makefile,
modName,
'0.0.1'
);
await writeExtensionControlFile(
info.controlFile,
modName,
answers.extensions.map((a:any)=>a.name),
'0.0.1'
);

process.chdir(cur);
return { ...argv, ...answers };
}
Loading