Compare commits

...

4 Commits

Author SHA1 Message Date
Jani Eväkallio
265ab19e31 Prototype basic publish command (just pushes all changes to git) 2020-07-16 11:57:47 +01:00
Ankit Tiwari
3ef95628f5 Remove posttest command 2020-07-16 13:33:18 +05:30
Ankit Tiwari
626f64aec0 Propagate the error from writeFileToDisk method 2020-07-16 13:32:30 +05:30
Ankit Tiwari
e36c285764 Refactor renameFile to use path module 2020-07-16 13:30:26 +05:30
4 changed files with 71 additions and 11 deletions

View File

@@ -62,10 +62,9 @@
"scripts": {
"cli": "./bin/run",
"postpack": "rm -f oclif.manifest.json",
"posttest": "eslint . --ext .ts --config .eslintrc",
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
"test": "jest",
"version": "oclif-dev readme && git add README.md"
},
"types": "lib/index.d.ts"
}
}

View File

@@ -0,0 +1,63 @@
import {Command, flags} from '@oclif/command'
import { execSync } from 'child_process';
import * as ora from 'ora';
export default class Publish extends Command {
static description = 'Push all changes to git repository';
static examples = [
`$ foam publish -m "Optional log message"`,
]
static flags = {
message: flags.string({
char: 'm',
description: "optional message"
}),
remote: flags.string({
char: 'r',
description: "remote"
}),
branch: flags.string({
char: 'b',
description: "branch"
})
}
async execWithSpinner(command: string, message: string) {
const spinner = ora(message).start();
// @todo handle errors
const response = execSync(command).toString();
spinner.succeed(`${message} Done!`);
return response;
}
async printPublishInfo(remote: string) {
// @todo actually get this data from GH API
const [, remotePath] = execSync(`git remote get-url ${remote}`).toString().trim().split(':');
const [repo, org] = remotePath.split('/').reverse();
console.log('');
console.log(`🎉 Your changes will be available shortly at https://${org}.github.io/${repo.replace('.git', '')}`);
console.log('');
}
async run() {
const {flags} = this.parse(Publish);
// @todo improve
const message = flags.message || 'foam publish';
const remote = flags.remote || 'origin';
const branch = flags.branch || 'master';
await this.execWithSpinner(`git add -A`, 'Staging changes...');
await this.execWithSpinner(`git commit -m "${message}"`, 'Creating a commit...');
await this.execWithSpinner(`git push ${remote} ${branch}`, "Publishing...");
await this.printPublishInfo(remote);
}
}

View File

@@ -1,4 +1,5 @@
import * as fs from 'fs'
import * as fs from 'fs';
import * as path from 'path';
/**
*
@@ -6,9 +7,9 @@ import * as fs from 'fs'
* @param newFileName "new file name" without the extension
*/
export const renameFile = async (fileUri: string, newFileName: string) => {
const fileName = fileUri.split('/').pop();
const extension = fileName?.split('.').pop();
const newFileUri = fileUri.replace(`${fileName}`, `${newFileName}.${extension}`);
const dirName = path.dirname(fileUri);
const extension = path.extname(fileUri);
const newFileUri = path.join(dirName, `${newFileName}${extension}`);
return fs.promises.rename(fileUri, newFileUri);
}

View File

@@ -1,8 +1,5 @@
import * as fs from 'fs';
export const writeFileToDisk = async (fileUri: string, data: string): Promise<Boolean> => {
return fs.promises.writeFile(fileUri, data).then(() => true).catch(err => {
console.log('error while writing to file: ', err)
return false;
})
export const writeFileToDisk = async (fileUri: string, data: string) => {
return fs.promises.writeFile(fileUri, data);
}