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
18 changes: 17 additions & 1 deletion __tests__/env.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* eslint-disable no-magic-numbers */
import { resolve } from 'path';
import { testEnv } from '@technote-space/github-action-test-helper';
import { setEnv } from '../src/env';
import { setEnv, loadTokenFromEnv } from '../src/env';

const fixturesDir = resolve(__dirname, 'fixtures');

describe('setEnv', () => {
testEnv();
Expand Down Expand Up @@ -43,3 +46,16 @@ describe('setEnv', () => {
expect(process.env).toHaveProperty('INPUT_FETCH_DEPTH');
});
});

describe('loadTokenFromEnv', () => {
testEnv();

it('should load token', () => {
expect(loadTokenFromEnv(resolve(fixturesDir, 'test1'))).toBe('test-token1');
expect(loadTokenFromEnv(resolve(fixturesDir, 'test2'))).toBe('test-token2');
});

it('should not load token', () => {
expect(loadTokenFromEnv(resolve(fixturesDir, 'test3'))).toBeUndefined();
});
});
1 change: 1 addition & 0 deletions __tests__/fixtures/test1/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TOKEN=test-token1
1 change: 1 addition & 0 deletions __tests__/fixtures/test2/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
token=test-token2
11 changes: 11 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ describe('execute', () => {
process.argv = saveArgv;
});

it('should throw error', async() => {
process.argv = [
'node',
'index.js',
'-t',
'test/v1.2.3',
];

await expect(execute()).rejects.toThrow('<token> is required.');
});

it('should do nothing', async() => {
const mockExec = spyOnExec();
const mockStdout = spyOnStdout();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@technote-space/release-github-actions": "^3.0.4",
"commander": "^4.1.1",
"cosmiconfig": "^6.0.0",
"dotenv": "^8.2.0",
"js-yaml": "^3.13.1"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import dotenv from 'dotenv';
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import { Config } from './types';

export const loadTokenFromEnv = (dir: string): string | undefined => {
if (!existsSync(resolve(dir, '.env'))) {
return;
}

const config = dotenv.parse(readFileSync(resolve(dir, '.env')));
return config.token ?? config.TOKEN;
};

export const setEnv = (config: Config, token: string, workspace: string): void => {
process.env.INPUT_GITHUB_TOKEN = token;
process.env.GITHUB_ACTOR = config.owner;
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import commander from 'commander';
import { getContextArgs, getGitHelper } from './misc';
import { isValidContext, prepare, commit, push } from './wrapper';
import { getConfig } from './config';
import { setEnv } from './env';
import { setEnv, loadTokenFromEnv } from './env';

export const execute = async(): Promise<void> => {
commander
.requiredOption('--token <token>', 'token')
.requiredOption('-t, --tag <tag>', 'tag name')
.option('--token <token>', 'token')
.option('-b, --branch [branch]', 'branch name')
.option('-w, --workspace [workspace]', 'working directory name', '.')
.option('-p, --package [package]', 'package file directory name', process.cwd())
.option('-e, --env [env]', 'env file name', '.env')
.option('-n, --dry-run', 'show what would have been pushed')
.parse(process.argv);

const token = commander.token || loadTokenFromEnv(commander.package);
if (!token) {
throw new Error('<token> is required.');
}

const config = getConfig(commander.package);
const args = getContextArgs(commander.tag, commander.branch, config);
setEnv(config, commander.token, commander.workspace);
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1651,6 +1651,11 @@ dot-prop@^3.0.0:
dependencies:
is-obj "^1.0.0"

dotenv@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a"
integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==

ecc-jsbn@~0.1.1:
version "0.1.2"
resolved "https://registry.yarnpkg.com/ecc-jsbn/-/ecc-jsbn-0.1.2.tgz#3a83a904e54353287874c564b7549386849a98c9"
Expand Down