gasup 0.3.5 → 0.4.1

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/README.md CHANGED
@@ -34,12 +34,14 @@ This command compiles your code with tsc.
34
34
 
35
35
  ### Bundle
36
36
 
37
- Bundle with esbuild into one file. This allows you to use libraries that are not natively available in Google Apps Script.
37
+ Bundle with esbuild into one file.
38
38
 
39
39
  ```
40
40
  gasup --bundle
41
41
  ```
42
42
 
43
+ This allows you to use libraries that are not natively available in Google Apps Script.
44
+
43
45
  ### Push
44
46
 
45
47
  Simply push using "clasp push".
@@ -62,7 +64,6 @@ If you want to deploy webapp, appsscript is like below.
62
64
 
63
65
  ```json
64
66
  {
65
- ...
66
67
  "webapp": {
67
68
  "executeAs": "USER_DEPLOYING",
68
69
  "access": "ANYONE_ANONYMOUS"
@@ -75,19 +76,9 @@ If you want to deploy webapp, appsscript is like below.
75
76
  If you need to switch between different environments (e.g., development, staging, production), you can use the --env flag to modify the appsscript.json file accordingly.
76
77
 
77
78
  ```
78
- gasup --env <target>
79
+ gasup --env <envpath>
79
80
  ```
80
81
 
81
- Target options:
82
-
83
- - dev : Development environment
84
- - stag : Staging environment
85
- - prod : Production environment
86
-
87
- Default environment files:
88
-
89
- .env, .env.staging, .env.production
90
-
91
82
  Environment file details:
92
83
 
93
84
  ```env
@@ -100,7 +91,7 @@ GASUP_PARENT_ID=yyy,zzz
100
91
  Chan command is available like below.
101
92
 
102
93
  ```
103
- gasup --env dev --build --push --deploy
94
+ gasup --env .env.production --bundle --push --deploy
104
95
  ```
105
96
 
106
97
  ### Config
@@ -120,8 +111,6 @@ export default config;
120
111
 
121
112
  ```ts
122
113
  export interface Config {
123
- envPaths?: Record<Env, string>;
124
- claspJsonPath?: string;
125
114
  appsScriptJsonPath?: string;
126
115
  bundleEntries?: string[];
127
116
  bundleOutfile?: string;
package/dist/bin/gasup.js CHANGED
@@ -1,62 +1,51 @@
1
+ import { execSync } from 'child_process';
1
2
  import { program } from 'commander';
2
3
  import { build } from '../build.js';
3
4
  import { bundle } from '../bundle.js';
4
- import { changeEnv } from '../envFile/changeEnv.js';
5
- import { init } from '../init.js';
6
- import inquirer from 'inquirer';
7
- import { execSync } from 'child_process';
5
+ import { loadConfigWithDefault } from '../config.js';
8
6
  import { deploy } from '../deploy.js';
7
+ import { changeEnv } from '../envFile/changeEnv.js';
9
8
  program
10
- .option('--init', 'init')
11
- .option('--env <env>', 'change env')
9
+ .option('--config', 'display current configuration')
10
+ .option('--env <envpath>', 'apply env to clasp')
12
11
  .option('--bundle', 'bundle')
13
12
  .option('--build', 'build')
14
13
  .option('--push', 'push')
15
14
  .option('--deploy', 'deploy')
16
15
  .parse();
17
16
  async function main() {
18
- if (program.opts().init) {
19
- inquirer
20
- .prompt({
21
- type: 'confirm',
22
- name: 'do',
23
- message: 'This action will overwrite some files. Are you sure?',
24
- })
25
- .then((res) => {
26
- if (!res.do)
27
- return;
28
- console.log('init gasup');
29
- init();
30
- console.log('init done');
31
- });
17
+ const opts = program.opts();
18
+ const config = await loadConfigWithDefault();
19
+ if (opts.config) {
20
+ console.log({ config });
32
21
  return;
33
22
  }
34
- if (program.opts().env) {
35
- changeEnv(program.opts().env);
36
- console.log(`env changed to ${program.opts().env}`);
23
+ if (opts.env?.startsWith('--')) {
24
+ throw new Error('env option is required');
37
25
  }
38
- if (program.opts().build) {
26
+ if (opts.env) {
27
+ console.log(`apply ${opts.env} to clasp`);
28
+ changeEnv(opts.env, config);
29
+ }
30
+ if (opts.build) {
39
31
  console.log('build with tsc');
40
- build();
32
+ build(config);
41
33
  }
42
- if (program.opts().bundle) {
34
+ if (opts.bundle) {
43
35
  console.log('bundle with esbuild');
44
- await bundle();
36
+ await bundle(config);
45
37
  }
46
- if (program.opts().push) {
38
+ if (opts.push) {
47
39
  console.log('push');
48
40
  execSync('clasp push', { stdio: 'inherit' });
49
41
  }
50
- if (program.opts().deploy) {
42
+ if (opts.deploy) {
51
43
  console.log(`deploy`);
52
44
  deploy();
53
45
  }
54
46
  console.log('gasup done');
55
47
  }
56
- try {
57
- await main();
58
- }
59
- catch (err) {
48
+ main().catch((err) => {
60
49
  console.error(err.message);
61
50
  process.exit(1);
62
- }
51
+ });
package/dist/build.d.ts CHANGED
@@ -1 +1,2 @@
1
- export declare function build(): void;
1
+ import { Config } from './types.js';
2
+ export declare function build(config: Config): void;
package/dist/build.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import { execSync } from 'child_process';
2
2
  import { copyFileSync } from 'fs';
3
3
  import path from 'path';
4
- import { config } from './config.js';
5
- export function build() {
4
+ export function build(config) {
6
5
  execSync('tsc', { stdio: 'inherit' });
7
6
  copyFileSync(config.appsScriptJsonPath, path.join(config.distDir, 'appsscript.json'));
8
7
  }
package/dist/bundle.d.ts CHANGED
@@ -1 +1,2 @@
1
- export declare function bundle(): Promise<void>;
1
+ import { Config } from './types.js';
2
+ export declare function bundle(config: Config): Promise<void>;
package/dist/bundle.js CHANGED
@@ -2,13 +2,13 @@ import esbuild from 'esbuild';
2
2
  import { GasPlugin } from 'esbuild-gas-plugin';
3
3
  import fs from 'fs-extra';
4
4
  import path from 'path';
5
- import { config } from './config.js';
6
- export async function bundle() {
5
+ export async function bundle(config) {
6
+ const { bundleEntries, bundleOutfile, appsScriptJsonPath, distDir } = config;
7
7
  await esbuild.build({
8
- entryPoints: config.bundleEntries,
8
+ entryPoints: bundleEntries,
9
9
  bundle: true,
10
- outfile: config.bundleOutfile,
10
+ outfile: bundleOutfile,
11
11
  plugins: [GasPlugin],
12
12
  });
13
- fs.copyFileSync(config.appsScriptJsonPath, path.join(config.distDir, 'appsscript.json'));
13
+ fs.copyFileSync(appsScriptJsonPath, path.join(distDir, 'appsscript.json'));
14
14
  }
@@ -1,6 +1,2 @@
1
- export interface ClaspJson {
2
- scriptId?: string;
3
- rootDir?: string;
4
- parentId?: string[];
5
- }
6
- export declare function getClaspJson(): ClaspJson;
1
+ import { ClaspJson } from './types.js';
2
+ export declare function updateClaspJson(claspJsonPath: string, data: ClaspJson): void;
package/dist/claspJson.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import fs from 'fs-extra';
2
- import { config } from './config.js';
3
- export function getClaspJson() {
4
- const json = fs.readFileSync(config.claspJsonPath, 'utf-8');
5
- return JSON.parse(json ?? '{}');
2
+ export function updateClaspJson(claspJsonPath, data) {
3
+ const json = fs.readFileSync(claspJsonPath, 'utf-8');
4
+ const existingData = JSON.parse(json ?? '{}');
5
+ const newData = { ...existingData, ...data };
6
+ fs.writeFileSync(claspJsonPath, JSON.stringify(newData, null, 2));
6
7
  }
@@ -1,15 +1,10 @@
1
1
  import { Config } from './types.js';
2
2
  export declare const defaultConfig: Config;
3
- export declare const config: {
4
- envPaths: {
5
- dev: string;
6
- stag: string;
7
- prod: string;
8
- };
3
+ export declare function loadConfigWithDefault(): Promise<{
9
4
  claspJsonPath?: string;
10
5
  appsScriptJsonPath?: string;
11
6
  bundleEntries?: string[];
12
7
  bundleOutfile?: string;
13
8
  srcDir?: string;
14
9
  distDir?: string;
15
- };
10
+ }>;
@@ -3,41 +3,36 @@ import path from 'path';
3
3
  import tsnode from 'ts-node';
4
4
  const configFileName = 'gasup.config.ts';
5
5
  export const defaultConfig = {
6
- envPaths: {
7
- dev: '.env',
8
- stag: '.env.staging',
9
- prod: '.env.production',
10
- },
11
6
  claspJsonPath: '.clasp.json',
12
7
  appsScriptJsonPath: 'appsscript.json',
13
- bundleEntries: [path.join('main', 'src', 'index.ts')],
8
+ bundleEntries: [path.join('src', 'index.ts')],
14
9
  bundleOutfile: path.join('dist', 'bundle.js'),
15
10
  srcDir: 'src',
16
11
  distDir: 'dist',
17
12
  };
18
- export const config = loadConfigWithDefault();
19
- function loadConfigWithDefault() {
13
+ // export const config = loadConfigWithDefault();
14
+ export async function loadConfigWithDefault() {
20
15
  const configPath = path.join(process.cwd(), configFileName);
21
- const config = loadConfig(configPath);
16
+ const config = await loadConfig(configPath);
22
17
  return {
23
18
  ...defaultConfig,
24
19
  ...config,
25
- envPaths: { ...defaultConfig.envPaths, ...(config.envPaths ?? {}) },
26
20
  };
27
21
  }
28
- function loadConfig(configPath) {
22
+ async function loadConfig(configPath) {
29
23
  try {
30
24
  const configString = fs.readFileSync(configPath, 'utf-8');
31
- const compiledPath = path.join(__dirname, '__config_compiled.js');
25
+ const compiledPath = path.join(process.cwd(), '__config_compiled.js');
32
26
  const compiledCode = tsnode
33
27
  .create()
34
28
  .compile(configString, 'config_dummy.ts');
35
29
  fs.writeFileSync(compiledPath, compiledCode);
36
- const data = require(compiledPath);
30
+ const data = await import(compiledPath);
37
31
  fs.unlinkSync(compiledPath);
38
32
  return data.default;
39
33
  }
40
34
  catch (err) {
35
+ console.error(err);
41
36
  return {};
42
37
  }
43
38
  }
@@ -1,2 +1,2 @@
1
- import { Env } from '../types.js';
2
- export declare function changeEnv(env?: Env): void;
1
+ import { Config } from '../types.js';
2
+ export declare function changeEnv(envPath: string, config: Config): void;
@@ -1,26 +1,24 @@
1
1
  import fs from 'fs-extra';
2
- import { config } from '../config.js';
2
+ import { updateClaspJson } from '../claspJson.js';
3
3
  import { getEnvData } from './envFile.js';
4
- export function changeEnv(env = 'dev') {
5
- const envPath = config.envPaths[env];
4
+ export function changeEnv(envPath, config) {
6
5
  if (!envPath) {
7
6
  throw new Error(`envPath not found on ${envPath}`);
8
7
  }
9
- const claspJsonPath = config.claspJsonPath;
10
- for (const path of [envPath, claspJsonPath]) {
11
- if (!fs.existsSync(path)) {
12
- throw new Error(`${path} not found`);
13
- }
8
+ if (!fs.existsSync(envPath)) {
9
+ throw new Error(`${envPath} not found`);
14
10
  }
15
11
  const envData = getEnvData(envPath);
16
12
  const scriptId = envData.GASUP_SCRIPT_ID;
13
+ const parentIds = envData.GASUP_PARENT_ID;
17
14
  if (!scriptId) {
18
15
  throw new Error(`GASUP_SCRIPT_ID not found on ${envPath}`);
19
16
  }
20
- const data = {
17
+ if (!parentIds) {
18
+ throw new Error(`GASUP_PARENT_ID not found on ${envPath}`);
19
+ }
20
+ updateClaspJson(config.claspJsonPath, {
21
21
  scriptId,
22
- rootDir: config.distDir,
23
- parentId: envData.GASUP_PARENT_ID,
24
- };
25
- fs.writeFileSync(claspJsonPath, JSON.stringify(data, null, 2));
22
+ parentId: parentIds,
23
+ });
26
24
  }
@@ -1,4 +1,3 @@
1
1
  import { EnvObject } from '../types.js';
2
- export declare function initEnvFiles(): void;
3
2
  export declare function getEnvData(envPath: string): EnvObject;
4
3
  export declare function addToEnvFile(envPath: string, _items: Record<string, string>): void;
@@ -1,17 +1,6 @@
1
1
  import dotenv from 'dotenv';
2
2
  import fs from 'fs-extra';
3
- import { config } from '../config.js';
4
- import { getClaspJson } from './../claspJson.js';
5
3
  import { addToEnvString } from './addToEnvString.js';
6
- export function initEnvFiles() {
7
- const claspJson = getClaspJson();
8
- const envPath = config.envPaths['dev'];
9
- console.log({ claspJson });
10
- addToEnvFile(envPath, {
11
- GASUP_SCRIPT_ID: claspJson.scriptId,
12
- GASUP_PARENT_ID: claspJson.parentId?.join(','),
13
- });
14
- }
15
4
  export function getEnvData(envPath) {
16
5
  try {
17
6
  const envString = fs.readFileSync(envPath, 'utf-8');
package/dist/types.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  export type Env = 'dev' | 'stag' | 'prod';
2
2
  export interface Config {
3
- envPaths?: Record<Env, string>;
4
3
  claspJsonPath?: string;
5
4
  appsScriptJsonPath?: string;
6
5
  bundleEntries?: string[];
@@ -12,3 +11,8 @@ export interface EnvObject {
12
11
  GASUP_SCRIPT_ID?: string;
13
12
  GASUP_PARENT_ID?: string[];
14
13
  }
14
+ export interface ClaspJson {
15
+ scriptId?: string;
16
+ rootDir?: string;
17
+ parentId?: string[];
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gasup",
3
- "version": "0.3.5",
3
+ "version": "0.4.1",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -24,15 +24,11 @@
24
24
  "dependencies": {
25
25
  "commander": "^13.0.0",
26
26
  "dotenv": "^16.4.7",
27
- "envfile": "^7.1.0",
28
27
  "esbuild": "^0.24.2",
29
28
  "esbuild-gas-plugin": "^0.8.0",
30
29
  "fs-extra": "^11.3.0",
31
- "inquirer": "^12.3.2",
32
- "path": "^0.12.7",
33
30
  "remeda": "^2.19.2",
34
- "ts-node": "^10.9.2",
35
- "tsx": "^4.19.2"
31
+ "ts-node": "^10.9.2"
36
32
  },
37
33
  "devDependencies": {
38
34
  "@types/fs-extra": "^11.0.4",
@@ -1,6 +0,0 @@
1
- export interface AppsScript {
2
- scriptId: string;
3
- rootDir: string;
4
- }
5
- export declare function getAppsScript(): AppsScript;
6
- export declare function copyAppsScript(): void;
@@ -1,11 +0,0 @@
1
- import { copyFileSync } from 'fs';
2
- import fs from 'fs-extra';
3
- import path from 'path';
4
- import { config } from './config.js';
5
- export function getAppsScript() {
6
- const json = fs.readFileSync(config.appsScriptJsonPath, 'utf-8');
7
- return JSON.parse(json ?? '{}');
8
- }
9
- export function copyAppsScript() {
10
- copyFileSync(config.appsScriptJsonPath, path.join(config.distDir, 'appsscript.json'));
11
- }
@@ -1,2 +0,0 @@
1
- import { Env } from './config.js';
2
- export declare function changeEnv(env?: Env): void;
package/dist/changeEnv.js DELETED
@@ -1,27 +0,0 @@
1
- import fs from 'fs-extra';
2
- import { getConfig } from './config.js';
3
- import { getEnvData } from './envFile/envFile.js';
4
- export function changeEnv(env = 'dev') {
5
- const config = getConfig();
6
- const envPath = config.envPaths[env];
7
- if (!envPath) {
8
- throw new Error(`envPath not found on ${envPath}`);
9
- }
10
- const claspJsonPath = config.claspJsonPath;
11
- for (const path of [envPath, claspJsonPath]) {
12
- if (!fs.existsSync(path)) {
13
- throw new Error(`${path} not found`);
14
- }
15
- }
16
- const envData = getEnvData(envPath);
17
- const scriptId = envData.GASUP_SCRIPT_ID;
18
- if (!scriptId) {
19
- throw new Error(`GASUP_SCRIPT_ID not found on ${envPath}`);
20
- }
21
- const data = {
22
- scriptId,
23
- rootDir: config.distDir,
24
- parentId: envData.GASUP_PARENT_ID,
25
- };
26
- fs.writeFileSync(claspJsonPath, JSON.stringify(data, null, 2));
27
- }
package/dist/envFile.d.ts DELETED
@@ -1,3 +0,0 @@
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;
package/dist/envFile.js DELETED
@@ -1,60 +0,0 @@
1
- import fs from 'fs-extra';
2
- import { getClaspJson } from './claspJson.js';
3
- import { getConfig } from './config.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
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,56 +0,0 @@
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
- });
package/dist/gasup.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/dist/gasup.js DELETED
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const commander_1 = require("commander");
4
- const bundle_1 = require("./bundle");
5
- const changeEnv_1 = require("./changeEnv");
6
- commander_1.program
7
- .option('--env <env>', 'change env')
8
- .option('--bundle', 'bundle')
9
- .parse();
10
- async function main() {
11
- if (commander_1.program.opts().change) {
12
- await (0, changeEnv_1.changeEnv)(commander_1.program.opts().change);
13
- }
14
- if (commander_1.program.opts().bundle) {
15
- await (0, bundle_1.bundle)();
16
- }
17
- console.log('done');
18
- }
19
- main().catch((err) => {
20
- console.error(err.message);
21
- process.exit(1);
22
- });
package/dist/init.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function init(): void;
package/dist/init.js DELETED
@@ -1,6 +0,0 @@
1
- import { initEnvFiles } from './envFile/envFile.js';
2
- import { addGitIgnores } from './gitignore.js';
3
- export function init() {
4
- initEnvFiles();
5
- addGitIgnores();
6
- }