gasup 0.0.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Config.d.ts CHANGED
@@ -2,12 +2,12 @@ export type Env = 'dev' | 'stag' | 'prod';
2
2
  export interface Config {
3
3
  envPaths?: Record<Env, string>;
4
4
  claspJsonPath?: string;
5
- appScriptJsonPath?: string;
5
+ appsScriptJsonPath?: string;
6
6
  bundleEntries?: string[];
7
7
  bundleOutfile?: string;
8
8
  srcDir?: string;
9
9
  distDir?: string;
10
10
  }
11
11
  export declare const defaultConfig: Config;
12
- export declare function getConfig(): Promise<Config>;
13
- export declare function saveDefaultConfig(): Promise<void>;
12
+ export declare function initConfig(): void;
13
+ export declare function getConfig(): Config;
package/dist/Config.js CHANGED
@@ -5,23 +5,24 @@ const configPath = 'gasup.json';
5
5
  export const defaultConfig = {
6
6
  envPaths: {
7
7
  dev: '.env',
8
- stag: '.env.stag',
9
- prod: '.env.prod',
8
+ stag: '.env.staging',
9
+ prod: '.env.production',
10
10
  },
11
11
  claspJsonPath: '.clasp.json',
12
- appScriptJsonPath: 'appsscript.json',
13
- bundleEntries: [path.join('src', 'index.ts')],
12
+ appsScriptJsonPath: 'appsscript.json',
13
+ bundleEntries: [path.join('main', 'src', 'index.ts')],
14
14
  bundleOutfile: path.join('dist', 'bundle.js'),
15
15
  srcDir: 'src',
16
16
  distDir: 'dist',
17
17
  };
18
- export async function getConfig() {
18
+ export function initConfig() {
19
+ const config = getConfig();
20
+ fs.writeFileSync(path.join(process.cwd(), configPath), JSON.stringify(config, null, 2));
21
+ }
22
+ export function getConfig() {
19
23
  try {
20
- console.log('getConfig', configPath);
21
- // const require = createRequire(import.meta.url);
22
24
  const res = readFileSync(path.join(process.cwd(), configPath));
23
25
  const data = JSON.parse(res.toString());
24
- console.log('data', data);
25
26
  return {
26
27
  ...defaultConfig,
27
28
  ...data,
@@ -29,11 +30,6 @@ export async function getConfig() {
29
30
  };
30
31
  }
31
32
  catch (e) {
32
- console.error(e);
33
33
  return defaultConfig;
34
34
  }
35
35
  }
36
- export async function saveDefaultConfig() {
37
- const config = { ...defaultConfig };
38
- await fs.writeFile(path.join(process.cwd(), configPath), JSON.stringify(config, null, 2));
39
- }
@@ -0,0 +1,6 @@
1
+ export interface AppsScript {
2
+ scriptId: string;
3
+ rootDir: string;
4
+ }
5
+ export declare function getAppsScript(): AppsScript;
6
+ export declare function copyAppsScript(): void;
@@ -0,0 +1,13 @@
1
+ import { copyFileSync } from 'fs';
2
+ import { getConfig } from './config.js';
3
+ import fs from 'fs-extra';
4
+ import path from 'path';
5
+ export function getAppsScript() {
6
+ const config = getConfig();
7
+ const json = fs.readFileSync(config.appsScriptJsonPath, 'utf-8');
8
+ return JSON.parse(json ?? '{}');
9
+ }
10
+ export function copyAppsScript() {
11
+ const config = getConfig();
12
+ copyFileSync(config.appsScriptJsonPath, path.join(config.distDir, 'appsscript.json'));
13
+ }
package/dist/bin/gasup.js CHANGED
@@ -1,28 +1,50 @@
1
1
  import { program } from 'commander';
2
+ import { build } from '../build.js';
2
3
  import { bundle } from '../bundle.js';
3
4
  import { changeEnv } from '../changeEnv.js';
4
- import { saveDefaultConfig } from '../Config.js';
5
+ import { init } from '../init.js';
6
+ import inquirer from 'inquirer';
5
7
  program
6
8
  .option('--init', 'init')
7
9
  .option('--env <env>', 'change env')
8
10
  .option('--bundle', 'bundle')
11
+ .option('--build', 'build')
9
12
  .parse();
10
13
  async function main() {
11
14
  if (program.opts().init) {
12
- await init();
15
+ inquirer
16
+ .prompt({
17
+ type: 'confirm',
18
+ name: 'do',
19
+ message: 'This action will overwrite some files. Are you sure?',
20
+ })
21
+ .then((res) => {
22
+ if (!res.do)
23
+ return;
24
+ console.log('init gasup');
25
+ init();
26
+ console.log('init done');
27
+ });
28
+ return;
13
29
  }
14
- if (program.opts().change) {
15
- await changeEnv(program.opts().change);
30
+ if (program.opts().env) {
31
+ changeEnv(program.opts().env);
32
+ console.log(`env changed to ${program.opts().env}`);
33
+ }
34
+ if (program.opts().build) {
35
+ console.log('build with tsc');
36
+ build();
16
37
  }
17
38
  if (program.opts().bundle) {
39
+ console.log('bundle with esbuild');
18
40
  await bundle();
19
41
  }
20
- console.log('done');
42
+ console.log('gasup done');
43
+ }
44
+ try {
45
+ await main();
21
46
  }
22
- main().catch((err) => {
47
+ catch (err) {
23
48
  console.error(err.message);
24
49
  process.exit(1);
25
- });
26
- async function init() {
27
- await saveDefaultConfig();
28
50
  }
@@ -0,0 +1 @@
1
+ export declare function build(): void;
package/dist/build.js ADDED
@@ -0,0 +1,9 @@
1
+ import { execSync } from 'child_process';
2
+ import { copyFileSync } from 'fs';
3
+ import path from 'path';
4
+ import { getConfig } from './config.js';
5
+ export function build() {
6
+ execSync('tsc', { stdio: 'inherit' });
7
+ const config = getConfig();
8
+ copyFileSync(config.appsScriptJsonPath, path.join(config.distDir, 'appsscript.json'));
9
+ }
package/dist/bundle.js CHANGED
@@ -1,20 +1,15 @@
1
- import { getConfig } from './Config.js';
1
+ import { getConfig } from './config.js';
2
2
  import esbuild from 'esbuild';
3
3
  import { GasPlugin } from 'esbuild-gas-plugin';
4
4
  import fs from 'fs-extra';
5
5
  import path from 'path';
6
6
  export async function bundle() {
7
- console.log('bundle with esbuild');
8
- const config = await getConfig();
9
- await esbuild
10
- .build({
7
+ const config = getConfig();
8
+ await esbuild.build({
11
9
  entryPoints: config.bundleEntries,
12
10
  bundle: true,
13
11
  outfile: config.bundleOutfile,
14
12
  plugins: [GasPlugin],
15
- })
16
- .catch((e) => {
17
- throw new Error(e);
18
13
  });
19
- fs.copyFileSync(config.appScriptJsonPath, path.join(config.distDir, 'appsscript.json'));
14
+ fs.copyFileSync(config.appsScriptJsonPath, path.join(config.distDir, 'appsscript.json'));
20
15
  }
@@ -1,2 +1,2 @@
1
- import { Env } from './Config.js';
2
- export declare function changeEnv(env?: Env): Promise<void>;
1
+ import { Env } from './config.js';
2
+ export declare function changeEnv(env?: Env): void;
package/dist/changeEnv.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import dotenv from 'dotenv';
2
2
  import fs from 'fs-extra';
3
- import { getConfig } from './Config.js';
4
- export async function changeEnv(env = 'dev') {
5
- const config = await getConfig();
3
+ import { getConfig } from './config.js';
4
+ export function changeEnv(env = 'dev') {
5
+ const config = getConfig();
6
6
  const envPath = config.envPaths[env];
7
7
  if (!envPath) {
8
8
  throw new Error(`envPath not found on ${envPath}`);
@@ -14,14 +14,13 @@ export async function changeEnv(env = 'dev') {
14
14
  }
15
15
  }
16
16
  dotenv.config({ path: envPath });
17
- const scriptId = process.env.SCRIPT_ID;
17
+ const scriptId = process.env.GASUP_SCRIPT_ID;
18
18
  if (!scriptId) {
19
- throw new Error(`SCRIPT_ID not found on ${envPath}`);
19
+ throw new Error(`GASUP_SCRIPT_ID not found on ${envPath}`);
20
20
  }
21
21
  const data = {
22
22
  scriptId,
23
23
  rootDir: config.distDir,
24
24
  };
25
25
  fs.writeFileSync(claspJsonPath, JSON.stringify(data, null, 2));
26
- console.log(`env changed to ${env}`);
27
26
  }
@@ -0,0 +1 @@
1
+ export declare function getClaspJson(): any;
@@ -0,0 +1,7 @@
1
+ import { getConfig } from './config.js';
2
+ import fs from 'fs-extra';
3
+ export function getClaspJson() {
4
+ const config = getConfig();
5
+ const json = fs.readFileSync(config.claspJsonPath, 'utf-8');
6
+ return JSON.parse(json ?? '{}');
7
+ }
@@ -0,0 +1,3 @@
1
+ export declare function initEnvFiles(): void;
2
+ export declare function addToEnvFile(envPath: string, _items: Record<string, string>): void;
3
+ export declare function addToEnvString(envString: string, _items: Record<string, string>): string;
@@ -0,0 +1,60 @@
1
+ import fs from 'fs-extra';
2
+ import { getConfig } from './config.js';
3
+ import { getClaspJson } from './claspJson.js';
4
+ export function initEnvFiles() {
5
+ const config = getConfig();
6
+ const claspJson = getClaspJson();
7
+ const scriptId = claspJson.scriptId;
8
+ const envPath = config.envPaths['dev'];
9
+ addToEnvFile(envPath, {
10
+ GASUP_SCRIPT_ID: scriptId,
11
+ });
12
+ }
13
+ export function addToEnvFile(envPath, _items) {
14
+ let envString = '';
15
+ try {
16
+ envString = fs.readFileSync(envPath, 'utf-8');
17
+ }
18
+ catch (e) { }
19
+ const newEnvString = addToEnvString(envString.trim(), _items);
20
+ fs.writeFileSync(envPath, newEnvString);
21
+ }
22
+ // 環境変数を変更する
23
+ export function addToEnvString(envString, _items) {
24
+ const items = { ..._items };
25
+ const newLines = [];
26
+ for (const line of envString.split('\n')) {
27
+ if (isCommentLine(line)) {
28
+ newLines.push(line);
29
+ }
30
+ else {
31
+ const { key, value } = parseLine(line);
32
+ if (key) {
33
+ if (items[key]) {
34
+ newLines.push(`${key}=${items[key]}`);
35
+ delete items[key];
36
+ }
37
+ else {
38
+ newLines.push(`${key}=${value}`);
39
+ }
40
+ }
41
+ else {
42
+ newLines.push('');
43
+ }
44
+ }
45
+ }
46
+ newLines.push('');
47
+ for (const [key, value] of Object.entries(items)) {
48
+ if (!key)
49
+ continue;
50
+ newLines.push(`${key}=${value}`);
51
+ }
52
+ return newLines.join('\n');
53
+ }
54
+ function isCommentLine(line) {
55
+ return line.startsWith('#');
56
+ }
57
+ function parseLine(line) {
58
+ const [key, ...values] = line.split('=');
59
+ return { key, value: values.join('=') };
60
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,56 @@
1
+ import { addToEnvString } from './envFile.js';
2
+ import { describe, it, expect } from 'vitest';
3
+ describe('addToEnvString', () => {
4
+ const mockEnvString = `
5
+ # This is a comment
6
+ KEY1=value1
7
+ KEY2=value2
8
+ `.trim();
9
+ it('should add new keys to the env string and overwrite existing ones', () => {
10
+ const itemsToAdd = {
11
+ KEY3: 'value3',
12
+ KEY1: 'newValue1', // 既存のKEY1を上書きしない
13
+ };
14
+ const result = addToEnvString(mockEnvString, itemsToAdd);
15
+ // 新しい環境変数を含んだ結果を確認
16
+ const expectedContent = `
17
+ # This is a comment
18
+ KEY1=value1
19
+ KEY2=value2
20
+
21
+ KEY3=value3
22
+ `.trim();
23
+ console.log({ mockEnvString, result, expectedContent });
24
+ expect(result).toBe(expectedContent);
25
+ });
26
+ it('should add only new keys if not already in the string', () => {
27
+ const itemsToAdd = {
28
+ KEY3: 'value3',
29
+ KEY4: 'value4',
30
+ };
31
+ const result = addToEnvString(mockEnvString, itemsToAdd);
32
+ // 新しい環境変数を含んだ結果を確認
33
+ const expectedContent = `
34
+ # This is a comment
35
+ KEY1=value1
36
+ KEY2=value2
37
+
38
+ KEY3=value3
39
+ KEY4=value4
40
+ `.trim();
41
+ expect(result).toBe(expectedContent);
42
+ });
43
+ it('should handle an empty env string', () => {
44
+ const itemsToAdd = {
45
+ KEY1: 'value1',
46
+ KEY2: 'value2',
47
+ };
48
+ const result = addToEnvString('', itemsToAdd);
49
+ // 空の環境変数文字列に対して追加された内容を確認
50
+ const expectedContent = `
51
+ KEY1=value1
52
+ KEY2=value2
53
+ `.trim();
54
+ expect(result.trim()).toBe(expectedContent);
55
+ });
56
+ });
@@ -0,0 +1 @@
1
+ export declare function addGitIgnores(): void;
@@ -0,0 +1,18 @@
1
+ import path from 'path';
2
+ import fs from 'fs-extra';
3
+ import * as R from 'remeda';
4
+ export function addGitIgnores() {
5
+ const filePath = path.join(process.cwd(), '.gitignore');
6
+ const existsIgnores = read(filePath);
7
+ const ignores = ['node_modules', '.DS_Store', '.env*', '.clasp.json'];
8
+ const newIgnores = R.unique([...existsIgnores, ...ignores]);
9
+ fs.writeFileSync(filePath, newIgnores.join('\n'));
10
+ }
11
+ function read(filePath) {
12
+ try {
13
+ return fs.readFileSync(filePath).toString().split('\n');
14
+ }
15
+ catch (err) {
16
+ return [];
17
+ }
18
+ }
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from './bundle.js';
2
2
  export * from './changeEnv.js';
3
- export * from './Config.js';
3
+ export * from './config.js';
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export * from './bundle.js';
2
2
  export * from './changeEnv.js';
3
- export * from './Config.js';
3
+ export * from './config.js';
package/dist/init.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function init(): void;
package/dist/init.js ADDED
@@ -0,0 +1,8 @@
1
+ import { initConfig } from './config.js';
2
+ import { initEnvFiles } from './envFile.js';
3
+ import { addGitIgnores } from './gitignore.js';
4
+ export function init() {
5
+ initConfig();
6
+ initEnvFiles();
7
+ addGitIgnores();
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gasup",
3
- "version": "0.0.1",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "gasup": "./dist/bin/gasup.js"
@@ -19,16 +19,20 @@
19
19
  "dependencies": {
20
20
  "commander": "^13.0.0",
21
21
  "dotenv": "^16.4.7",
22
+ "envfile": "^7.1.0",
22
23
  "esbuild": "^0.24.2",
23
24
  "esbuild-gas-plugin": "^0.8.0",
24
25
  "fs-extra": "^11.3.0",
26
+ "inquirer": "^12.3.2",
25
27
  "path": "^0.12.7",
28
+ "remeda": "^2.19.2",
26
29
  "ts-node": "^10.9.2",
27
30
  "tsx": "^4.19.2"
28
31
  },
29
32
  "devDependencies": {
30
33
  "@types/fs-extra": "^11.0.4",
31
- "@types/node": "^22.10.7"
34
+ "@types/node": "^22.10.7",
35
+ "vitest": "^3.0.2"
32
36
  },
33
37
  "scripts": {
34
38
  "build": "tsc",