Skip to content

Commit 23ed1f6

Browse files
Merge pull request #10 from technote-space/release/v0.0.6
Release/v0.0.6
2 parents ec5fda3 + 9fc73d8 commit 23ed1f6

File tree

4 files changed

+87
-35
lines changed

4 files changed

+87
-35
lines changed

.github/workflows/released.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#on: release
2-
on: project_card
1+
on: release
32
name: Released
43
jobs:
54
release:

__tests__/utils/misc.test.ts

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,46 @@ describe('getWorkspace', () => {
166166
describe('getBuildCommands', () => {
167167
testEnv();
168168

169-
it('should get build commands', () => {
169+
it('should get build commands 1', () => {
170170
process.env.INPUT_BUILD_COMMAND = 'test';
171-
expect(getBuildCommands()).toEqual([
171+
expect(getBuildCommands(path.resolve(__dirname, '..', 'fixtures', 'test4'))).toEqual([
172+
'yarn install',
172173
'test',
174+
'yarn build', // build command of package.json
175+
'yarn install --production',
173176
]);
174177
});
175178

176-
it('should get empty', () => {
177-
expect(getBuildCommands()).toEqual([]);
179+
it('should get build commands 2', () => {
180+
process.env.INPUT_BUILD_COMMAND = 'yarn build';
181+
expect(getBuildCommands(path.resolve(__dirname, '..', 'fixtures', 'test4'))).toEqual([
182+
'yarn install',
183+
'yarn build',
184+
'yarn install --production',
185+
]);
186+
});
187+
188+
it('should get build commands 3', () => {
189+
process.env.INPUT_BUILD_COMMAND = 'yarn install && yarn build';
190+
expect(getBuildCommands(path.resolve(__dirname, '..', 'fixtures', 'test4'))).toEqual([
191+
'yarn install',
192+
'yarn build',
193+
]);
194+
});
195+
196+
it('should get build commands 4', () => {
197+
process.env.INPUT_BUILD_COMMAND = 'test';
198+
expect(getBuildCommands(path.resolve(__dirname, '..', 'fixtures', 'test1'))).toEqual([
199+
'yarn install',
200+
'test',
201+
'yarn install --production',
202+
]);
203+
});
204+
205+
it('should get build commands 5', () => {
206+
expect(getBuildCommands(path.resolve(__dirname, '..', 'fixtures', 'test1'))).toEqual([
207+
'yarn install --production',
208+
]);
178209
});
179210
});
180211

@@ -218,15 +249,15 @@ describe('detectBuildCommand', () => {
218249
});
219250

220251
it('should detect build command 1', () => {
221-
expect(detectBuildCommand(path.resolve(__dirname, '..', 'fixtures', 'test4'))).toBe('test1');
252+
expect(detectBuildCommand(path.resolve(__dirname, '..', 'fixtures', 'test4'))).toBe('build');
222253
});
223254

224255
it('should detect build command 1', () => {
225-
expect(detectBuildCommand(path.resolve(__dirname, '..', 'fixtures', 'test5'))).toBe('test2');
256+
expect(detectBuildCommand(path.resolve(__dirname, '..', 'fixtures', 'test5'))).toBe('production');
226257
});
227258

228259
it('should detect build command 1', () => {
229-
expect(detectBuildCommand(path.resolve(__dirname, '..', 'fixtures', 'test6'))).toBe('test3');
260+
expect(detectBuildCommand(path.resolve(__dirname, '..', 'fixtures', 'test6'))).toBe('prod');
230261
});
231262
});
232263

src/utils/command.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {getGitUrl, getRepository, getBuildCommands, getWorkspace, getCommitMessa
88
export const deploy = async (branch: string, context: Context) => {
99
const workDir = path.resolve(getWorkspace(), '.work');
1010
const buildDir = path.resolve(workDir, 'build');
11-
const pushDir = path.resolve(workDir, 'build');
11+
const pushDir = path.resolve(workDir, 'push');
1212
signale.info(`Deploying branch %s to %s`, branch, getRepository(context));
1313

1414
fs.mkdirSync(pushDir, {recursive: true});
@@ -33,6 +33,23 @@ const cloneForBranch = async (pushDir: string, branch: string, context: Context)
3333

3434
const url = getGitUrl(context);
3535
await execAsync(`git -C ${pushDir} clone --quiet --branch=${branch} --depth=1 ${url} .`, true, 'git clone', true);
36+
if (!fs.existsSync(path.resolve(pushDir, '.git'))) {
37+
await gitInit(pushDir);
38+
await gitCheckout(pushDir, branch);
39+
}
40+
};
41+
42+
const gitInit = async (pushDir: string) => {
43+
signale.info('Initializing local git repo');
44+
45+
await execAsync(`git -C ${pushDir} init .`);
46+
47+
};
48+
49+
const gitCheckout = async (pushDir: string, branch: string) => {
50+
signale.info('Checking out orphan branch %s', branch);
51+
52+
await execAsync(`git -C ${pushDir} checkout --orphan "${branch}"`);
3653
};
3754

3855
const config = async (pushDir: string) => {
@@ -69,22 +86,9 @@ const cloneForBuild = async (buildDir: string, context: Context) => {
6986

7087
const runBuild = async (buildDir: string) => {
7188
signale.info('=== Running build for release ===');
72-
let commands = getBuildCommands();
73-
const buildCommand = detectBuildCommand(buildDir);
74-
const hasInstallCommand = commands.filter(command => command.includes('npm run install') || command.includes('yarn install')).length > 0;
75-
if (!hasInstallCommand) {
76-
commands.push('yarn install');
77-
}
78-
if (typeof buildCommand === 'string') {
79-
commands = commands.filter(command => !buildCommand.startsWith(`npm run ${command}`) && !buildCommand.startsWith(`yarn ${command}`));
80-
commands.push(`yarn ${buildCommand}`);
81-
}
82-
if (!hasInstallCommand) {
83-
commands.push('yarn install --production');
84-
}
8589

8690
const current = process.cwd();
87-
for (const command of commands) {
91+
for (const command of getBuildCommands(buildDir)) {
8892
await execAsync(`cd ${buildDir} && ${command}`);
8993
}
9094
await execAsync(`cd ${current}`);
@@ -97,14 +101,13 @@ const copyFiles = async (buildDir: string, pushDir: string) => {
97101
};
98102

99103
const execAsync = (command: string, quiet: boolean = false, altCommand: string | null = null, suppressError: boolean = false) => new Promise<string>((resolve, reject) => {
100-
if (quiet && 'string' === typeof altCommand) signale.info(`Run command: ${altCommand}`);
101-
if (!quiet) signale.info(`Run command: ${command}`);
104+
if ('string' === typeof altCommand) signale.info(`Run command: ${altCommand}`);
105+
else if (!quiet) signale.info(`Run command: ${command}`);
102106
exec(command + (quiet ? ' > /dev/null 2>&1' : '') + (suppressError ? ' || :' : ''), (error, stdout) => {
103107
if (error) {
104-
if (quiet) {
105-
if ('string' === typeof altCommand) reject(new Error(`command [${altCommand}] exited with code ${error.code}.`));
106-
else reject(new Error(`command exited with code ${error.code}.`));
107-
} else reject(new Error(`command [${command}] exited with code ${error.code}.`));
108+
if ('string' === typeof altCommand) reject(new Error(`command [${altCommand}] exited with code ${error.code}.`));
109+
else if (!quiet) reject(new Error(`command [${command}] exited with code ${error.code}.`));
110+
else reject(new Error(`command exited with code ${error.code}.`));
108111
} else {
109112
if (!quiet) console.log(stdout);
110113
resolve(stdout);

src/utils/misc.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,34 @@ export const isTargetEvent = (context: Context): boolean => TARGET_EVENT_NAME ==
99

1010
export const parseConfig = (content: string): object => yaml.safeLoad(Buffer.from(content, 'base64').toString()) || {};
1111

12-
export const getRepository = (context: Context):string => `${context.repo.owner}/${context.repo.repo}`;
12+
export const getRepository = (context: Context): string => `${context.repo.owner}/${context.repo.repo}`;
1313

1414
export const getGitUrl = (context: Context): string => {
1515
const token = getAccessToken();
1616
return `https://${token}@github.com/${context.repo.owner}/${context.repo.repo}.git`;
1717
};
1818

19-
export const getBuildCommands = (): string[] => {
19+
export const getBuildCommands = (dir: string): string[] => {
2020
const command = getInput('BUILD_COMMAND');
21-
if ('' === command) return [];
22-
return command.split('&&').map(str => str.trim().replace(/\s{2,}/g, ' '));
21+
let commands = '' === command ? [] : command.split('&&').map(normalizeCommand);
22+
23+
const buildCommand = detectBuildCommand(dir);
24+
const hasInstallCommand = commands.filter(command => command.includes('npm run install') || command.includes('yarn install')).length > 0;
25+
26+
if (typeof buildCommand === 'string') {
27+
commands = commands.filter(command => !command.startsWith(`npm run ${buildCommand}`) && !command.startsWith(`yarn ${buildCommand}`));
28+
commands.push(`yarn ${buildCommand}`);
29+
}
30+
31+
if (!hasInstallCommand && commands.length > 0) {
32+
commands.unshift('yarn install');
33+
}
34+
35+
if (!hasInstallCommand) {
36+
commands.push('yarn install --production');
37+
}
38+
39+
return commands;
2340
};
2441

2542
export const getAccessToken = (): string => getInput('ACCESS_TOKEN', {required: true});
@@ -45,8 +62,10 @@ export const detectBuildCommand = (dir: string): boolean | string => {
4562

4663
const scripts = parsed['scripts'];
4764
for (const target of SEARCH_BUILD_COMMAND_TARGETS) {
48-
if (target in scripts) return scripts[target].trim().replace(/\s{2,}/g, ' ');
65+
if (target in scripts) return normalizeCommand(target);
4966
}
5067

5168
return false;
5269
};
70+
71+
const normalizeCommand = (command: string): string => command.trim().replace(/\s{2,}/g, ' ');

0 commit comments

Comments
 (0)