diff --git a/__tests__/env.test.ts b/__tests__/env.test.ts index d365a4b..309a94a 100644 --- a/__tests__/env.test.ts +++ b/__tests__/env.test.ts @@ -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(); @@ -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(); + }); +}); diff --git a/__tests__/fixtures/test1/.env b/__tests__/fixtures/test1/.env new file mode 100644 index 0000000..9204d61 --- /dev/null +++ b/__tests__/fixtures/test1/.env @@ -0,0 +1 @@ +TOKEN=test-token1 \ No newline at end of file diff --git a/__tests__/fixtures/test2/.env b/__tests__/fixtures/test2/.env new file mode 100644 index 0000000..5f9df67 --- /dev/null +++ b/__tests__/fixtures/test2/.env @@ -0,0 +1 @@ +token=test-token2 \ No newline at end of file diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 91d1c5d..1d8fb45 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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(' is required.'); + }); + it('should do nothing', async() => { const mockExec = spyOnExec(); const mockStdout = spyOnStdout(); diff --git a/package.json b/package.json index 5be7680..db5b887 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/env.ts b/src/env.ts index 47d6464..c7d077d 100644 --- a/src/env.ts +++ b/src/env.ts @@ -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; diff --git a/src/index.ts b/src/index.ts index d63f36e..5dbb8ce 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 => { commander - .requiredOption('--token ', 'token') .requiredOption('-t, --tag ', 'tag name') + .option('--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(' is required.'); + } + const config = getConfig(commander.package); const args = getContextArgs(commander.tag, commander.branch, config); setEnv(config, commander.token, commander.workspace); diff --git a/yarn.lock b/yarn.lock index d0e0f19..aff8159 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"