gasup 0.0.1 → 0.1.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',
12
+ appsScriptJsonPath: 'appsscript.json',
13
13
  bundleEntries: [path.join('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,
@@ -33,7 +34,3 @@ export async function getConfig() {
33
34
  return defaultConfig;
34
35
  }
35
36
  }
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,37 @@
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';
5
6
  program
6
7
  .option('--init', 'init')
7
8
  .option('--env <env>', 'change env')
8
9
  .option('--bundle', 'bundle')
10
+ .option('--build', 'build')
9
11
  .parse();
10
- async function main() {
12
+ function main() {
11
13
  if (program.opts().init) {
12
- await init();
14
+ console.log('init gasup');
15
+ init();
13
16
  }
14
- if (program.opts().change) {
15
- await changeEnv(program.opts().change);
17
+ if (program.opts().env) {
18
+ changeEnv(program.opts().env);
19
+ console.log(`env changed to ${program.opts().env}`);
20
+ }
21
+ if (program.opts().build) {
22
+ console.log('build with tsc');
23
+ build();
16
24
  }
17
25
  if (program.opts().bundle) {
18
- await bundle();
26
+ console.log('bundle with esbuild');
27
+ bundle();
19
28
  }
20
- console.log('done');
29
+ console.log('gasup done');
30
+ }
31
+ try {
32
+ main();
21
33
  }
22
- main().catch((err) => {
34
+ catch (err) {
23
35
  console.error(err.message);
24
36
  process.exit(1);
25
- });
26
- async function init() {
27
- await saveDefaultConfig();
28
37
  }
@@ -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.d.ts CHANGED
@@ -1 +1 @@
1
- export declare function bundle(): Promise<void>;
1
+ export declare function bundle(): void;
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
- export async function bundle() {
7
- console.log('bundle with esbuild');
8
- const config = await getConfig();
9
- await esbuild
10
- .build({
6
+ export function bundle() {
7
+ const config = getConfig();
8
+ esbuild.buildSync({
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}`);
@@ -23,5 +23,4 @@ export async function changeEnv(env = 'dev') {
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,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,49 @@
1
+ import fs from 'fs-extra';
2
+ import { getConfig } from './config.js';
3
+ import { getAppsScript } from './appsScript.js';
4
+ export function initEnvFiles() {
5
+ const config = getConfig();
6
+ const appsScript = getAppsScript();
7
+ const scriptId = appsScript.scriptId;
8
+ const envPath = config.envPaths['dev'];
9
+ addToEnvFile(envPath, {
10
+ SCRIPT_ID: scriptId,
11
+ });
12
+ }
13
+ export function addToEnvFile(envPath, _items) {
14
+ const envString = fs.readFileSync(envPath, 'utf-8');
15
+ const newEnvString = addToEnvString(envString, _items);
16
+ fs.writeFileSync(envPath, newEnvString);
17
+ }
18
+ // 環境変数を追加する。安全のため、既存の環境変数は上書きしない
19
+ export function addToEnvString(envString, _items) {
20
+ const items = { ..._items };
21
+ const newLines = [];
22
+ for (const line of envString.split('\n')) {
23
+ if (isCommentLine(line)) {
24
+ newLines.push(line);
25
+ }
26
+ else {
27
+ const { key, value } = parseLine(line);
28
+ if (key) {
29
+ newLines.push(`${key}=${value}`);
30
+ }
31
+ else {
32
+ newLines.push('');
33
+ }
34
+ delete items[key];
35
+ }
36
+ }
37
+ newLines.push('');
38
+ for (const [key, value] of Object.entries(items)) {
39
+ newLines.push(`${key}=${value}`);
40
+ }
41
+ return newLines.join('\n');
42
+ }
43
+ function isCommentLine(line) {
44
+ return line.startsWith('#');
45
+ }
46
+ function parseLine(line) {
47
+ const [key, ...values] = line.split('=');
48
+ return { key, value: values.join('=') };
49
+ }
@@ -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.1.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "gasup": "./dist/bin/gasup.js"
@@ -19,16 +19,19 @@
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",
25
26
  "path": "^0.12.7",
27
+ "remeda": "^2.19.2",
26
28
  "ts-node": "^10.9.2",
27
29
  "tsx": "^4.19.2"
28
30
  },
29
31
  "devDependencies": {
30
32
  "@types/fs-extra": "^11.0.4",
31
- "@types/node": "^22.10.7"
33
+ "@types/node": "^22.10.7",
34
+ "vitest": "^3.0.2"
32
35
  },
33
36
  "scripts": {
34
37
  "build": "tsc",