-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathdeploybutton.ts
66 lines (58 loc) · 2.52 KB
/
deploybutton.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
import { flags, SfdxCommand } from '@salesforce/command';
import { URL } from 'url';
import fs = require('fs-extra');
const startText = '<!-- launchButton -->';
const stopText = '<!-- launchButtonStop -->';
const regex = new RegExp(`(?<=${startText})(\n*.*\n*)(?=${stopText})`);
export default class DeployButton extends SfdxCommand {
public static description = 'modify your local readme file to include a deployer link/button';
protected static flagsConfig = {
deployer: flags.url({
char: 'd',
description: 'the base url for your deployer',
default: new URL('https://hosted-scratch.herokuapp.com'),
required: true
}),
button: flags.url({
char: 'b',
description: 'the public url where your button lives',
default: new URL('https://raw.githubusercontent.com/mshanemc/deploy-to-sfdx/master/client-src/resources/images/sfdx_it_now.png'),
required: true
})
// type: flags.string({ char: 't', description: 'want a link or a button?', required: true, default: 'button' })
};
protected static requiresProject = true;
public async run(): Promise<any> {
// get repo url from package.json
const repoUrl = (await fs.readJSON('package.json')).repository.url.replace('git+', '').replace('.git', '');
// this.ux.log(repoUrl);
if (!repoUrl) {
throw new Error(`a repo url was not found in your project's package.json file`);
}
// read in the README
let readmeContents = await fs.readFile('README.md', 'utf-8');
// check for existing button code, replace if found
if (readmeContents.match(regex)) {
// console.log('already has a button...will try to replace');q
readmeContents = readmeContents.replace(regex, this.stringBuilder(repoUrl, this.flags.deployer.toString(), false));
} else {
// add code for button at top of repo
readmeContents = `
${this.stringBuilder(repoUrl, this.flags.deployer.toString(), true)}
${readmeContents}`;
}
await fs.writeFile('README.md', readmeContents);
}
private stringBuilder = (repository: string, deployerURL: string, full: boolean) => {
if (full) {
return `
${startText}
[](${deployerURL}launch?template=${repository}) <${repository}>
${stopText}
`;
}
return `
[](${deployerURL}launch?template=${repository}) <${repository}>
`;
};
}