Skip to content

Commit

Permalink
chore: move from gts to xo (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jul 28, 2023
1 parent fcb7cab commit c52514e
Show file tree
Hide file tree
Showing 12 changed files with 601 additions and 587 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

3 changes: 0 additions & 3 deletions .eslintrc.json

This file was deleted.

3 changes: 0 additions & 3 deletions .prettierignore

This file was deleted.

6 changes: 0 additions & 6 deletions .prettierrc.json

This file was deleted.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
},
"scripts": {
"test": "c8 mocha build/test",
"lint": "gts check",
"lint": "xo --prettier",
"compile": "tsc -p .",
"fix": "gts fix",
"fix": "xo --prettier --fix",
"prepare": "npm run compile",
"pretest": "npm run compile",
"watch": "tsc -p . --watch"
Expand Down Expand Up @@ -53,12 +53,12 @@
"@types/update-notifier": "^6.0.1",
"c8": "^7.10.0",
"gaxios": "^5.0.0",
"gts": "^3.1.0",
"mocha": "^10.0.0",
"nock": "^13.2.1",
"semantic-release": "^21.0.0",
"sinon": "^15.0.0",
"typescript": "~5.0.0"
"typescript": "~5.0.0",
"xo": "^0.55.0"
},
"c8": {
"exclude": [
Expand Down
176 changes: 89 additions & 87 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
#!/usr/bin/env node
import util from 'node:util';
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import meow from 'meow';
import updateNotifier from 'update-notifier';
import updateNotifier, {type Package} from 'update-notifier';
import ora from 'ora';
import chalk from 'chalk';
import util from 'util';
import fs from 'fs';
import path from 'path';
import {URL} from 'url';

import {Builder, BuildOptions, ProgressEvent} from './index.js';
import {Builder, type BuildOptions, ProgressEvent} from './index.js';

const pkg = JSON.parse(
fs.readFileSync(new URL('../../package.json', import.meta.url), 'utf-8')
);
fs.readFileSync(new URL('../../package.json', import.meta.url), 'utf8'),
) as Package;
updateNotifier({pkg}).notify();

const cli = meow(
`
`
Usage
$ gcb [SOURCE] [--flags]
Expand All @@ -40,99 +39,102 @@ const cli = meow(
$ gcb --config ../perfect.yaml --tag ohai123
$ gcp containers/web
`,
{
importMeta: import.meta,
flags: {
config: {type: 'string'},
tag: {type: 'string'},
},
}
{
importMeta: import.meta,
flags: {
config: {type: 'string'},
tag: {type: 'string'},
},
},
);

async function main() {
if (cli.input.length > 1) {
cli.showHelp();
return;
}

const start = Date.now();
const opts = cli.flags as BuildOptions;
opts.sourcePath = cli.input.length > 0 ? cli.input[0] : process.cwd();
if (!path.isAbsolute(opts.sourcePath)) {
opts.sourcePath = path.join(process.cwd(), opts.sourcePath);
}
const hasIgnore = await hasIgnoreFile(opts.sourcePath);
if (!hasIgnore) {
await generateIgnoreFile(opts.sourcePath);
}
const spinny = ora('Initializing build...').start();
const builder = new Builder(opts);
builder
.on(ProgressEvent.CREATING_BUCKET, bucket => {
spinny.stopAndPersist({
symbol: '🌧',
text: `Bucket '${bucket}' created.`,
});
spinny.start('Packing and uploading sources...');
})
.on(ProgressEvent.UPLOADING, () => {
spinny.stopAndPersist({symbol: '📦', text: 'Source code packaged.'});
spinny.start('Uploading source...');
})
.on(ProgressEvent.BUILDING, () => {
spinny.stopAndPersist({
symbol: '🛸',
text: 'Source uploaded to cloud.',
});
spinny.start('Building container...');
})
.on(ProgressEvent.LOG, data => {
console.error('\n\n' + chalk.gray(data));
})
.on(ProgressEvent.COMPLETE, () => {
const seconds = (Date.now() - start) / 1000;
spinny.stopAndPersist({
symbol: '🚀',
text: `Container built in ${seconds} seconds.`,
});
});
try {
await builder.build();
} catch (e) {
const err = e as Error;
console.error(err);
spinny.fail(err.message);
// eslint-disable-next-line no-process-exit
process.exit(1);
}
if (cli.input.length > 1) {
cli.showHelp();
return;
}

const start = Date.now();
const options = cli.flags as BuildOptions;
options.sourcePath = cli.input.length > 0 ? cli.input[0] : process.cwd();
if (!path.isAbsolute(options.sourcePath)) {
options.sourcePath = path.join(process.cwd(), options.sourcePath);
}

const hasIgnore = await hasIgnoreFile(options.sourcePath);
if (!hasIgnore) {
await generateIgnoreFile(options.sourcePath);
}

const spinny = ora('Initializing build...').start();
const builder = new Builder(options);
builder
.on(ProgressEvent.CREATING_BUCKET, (bucket) => {
spinny.stopAndPersist({
symbol: '🌧',
text: `Bucket '${bucket}' created.`,
});
spinny.start('Packing and uploading sources...');
})
.on(ProgressEvent.UPLOADING, () => {
spinny.stopAndPersist({symbol: '📦', text: 'Source code packaged.'});
spinny.start('Uploading source...');
})
.on(ProgressEvent.BUILDING, () => {
spinny.stopAndPersist({
symbol: '🛸',
text: 'Source uploaded to cloud.',
});
spinny.start('Building container...');
})
.on(ProgressEvent.LOG, (data) => {
console.error('\n\n' + chalk.gray(data));
})
.on(ProgressEvent.COMPLETE, () => {
const seconds = (Date.now() - start) / 1000;
spinny.stopAndPersist({
symbol: '🚀',
text: `Container built in ${seconds} seconds.`,
});
});
try {
await builder.build();
} catch (error) {
const error_ = error as Error;
console.error(error_);
spinny.fail(error_.message);

process.exit(1);
}
}

async function generateIgnoreFile(targetDir: string) {
console.log(`
console.log(`
🤖 I generated a '.gcloudignore' file in the target directory.
This file contains a list of glob patterns that should be ingored
in your build. It works just like a .gitignore file 💜
`);
await new Promise((resolve, reject) => {
fs.createReadStream(path.join(__dirname, '../../src/.gcloudignore'))
.pipe(fs.createWriteStream(path.join(targetDir, '.gcloudignore')))
.on('error', reject)
.on('close', resolve);
});
await new Promise((resolve, reject) => {
// eslint-disable-next-line unicorn/prefer-module
fs.createReadStream(path.join(__dirname, '../../src/.gcloudignore'))
.pipe(fs.createWriteStream(path.join(targetDir, '.gcloudignore')))
.on('error', reject)
.on('close', resolve);
});
}

/**
* Checks to see if a given directory has a `.gcloudignore` file.
* @param targetDir The directory with the sources to deploy.
*/
async function hasIgnoreFile(targetDir: string) {
const ignoreFile = path.join(targetDir, '.gcloudignore');
try {
await util.promisify(fs.stat)(ignoreFile);
return true;
} catch (e) {
return false;
}
const ignoreFile = path.join(targetDir, '.gcloudignore');
try {
await util.promisify(fs.stat)(ignoreFile);
return true;
} catch {
return false;
}
}

main().catch(console.error);
await main();
Loading

0 comments on commit c52514e

Please sign in to comment.