@mikarinneoracle/oci-cdk-code-only-preview 0.0.9 → 0.0.10

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
@@ -133,7 +133,7 @@ export OCI_FUNCTION_APP_NAME='my-existing-function-app'
133
133
 
134
134
  These settings are optional: `OCI_CODE_ONLY_SOURCE_DIR` (defaults to the current directory), `OCI_FUNCTION_MEMORY_MB` (from `func.yaml`, otherwise `256`), and `OCI_FUNCTION_TIMEOUT_SECONDS` (from `func.yaml`, otherwise `30`). `OCI_TENANCY_ID`, `OCI_REGION`, and `OCI_NAMESPACE` are also optional when they can be resolved from the active OCI CLI profile.
135
135
 
136
- The archive is built from `OCI_CODE_ONLY_SOURCE_DIR` (or the current directory) with the required `function/` directory at its ZIP root. It includes source files and excludes `node_modules`, `.git`, `.tools`, `.terraform`, and `cdktf.out`. The current preview path is function-only: it does not create an API Gateway because the CLI-managed function OCID is not in Terraform state.
136
+ Each deploy re-builds `<function-name>.zip` in the project root and uploads that file. The ZIP has the required `function/` directory at its root, includes source files, and excludes `node_modules`, `.git`, `.tools`, `.terraform`, and `cdktf.out`. A successful code-only destroy removes this ZIP after Terraform has destroyed the Function App and infrastructure. The current preview path is function-only: it does not create an API Gateway because the CLI-managed function OCID is not in Terraform state.
137
137
 
138
138
  Use the same flag or environment variable for deletion. OCDK deletes the CLI-managed function first, then lets Terraform destroy the Function App and its infrastructure:
139
139
 
@@ -67,12 +67,18 @@ function readFunctionMetadata() {
67
67
  return { functionName, appName, handler, runtimeName, memory, timeout };
68
68
  }
69
69
 
70
- function createArchive() {
70
+ function codeOnlyArchiveFileName(functionName) {
71
+ const safeName = functionName.replace(/[^A-Za-z0-9._-]/g, '-');
72
+ return `${safeName || 'function'}.zip`;
73
+ }
74
+
75
+ function createArchive(functionName) {
71
76
  if (!fs.existsSync(projectDir) || !fs.statSync(projectDir).isDirectory()) {
72
77
  fail(`source directory does not exist: ${projectDir}`);
73
78
  }
74
79
  const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ocdk-code-only-'));
75
- const archivePath = path.join(tempDir, 'function-source.zip');
80
+ const archiveFileName = codeOnlyArchiveFileName(functionName);
81
+ const archivePath = path.join(projectDir, archiveFileName);
76
82
  const archiveRoot = path.join(tempDir, 'function');
77
83
  const excludedTopLevel = new Set(['node_modules', '.git', '.tools', '.terraform', 'cdktf.out']);
78
84
  fs.cpSync(projectDir, archiveRoot, {
@@ -80,12 +86,15 @@ function createArchive() {
80
86
  filter: (sourcePath) => {
81
87
  const relativePath = path.relative(projectDir, sourcePath);
82
88
  if (!relativePath) return true;
89
+ if (relativePath === archiveFileName) return false;
83
90
  return !excludedTopLevel.has(relativePath.split(path.sep)[0]);
84
91
  },
85
92
  });
93
+ fs.rmSync(archivePath, { force: true });
86
94
  const zip = spawnSync('zip', ['-q', '-r', archivePath, 'function'], { cwd: tempDir, encoding: 'utf8', shell: false });
87
95
  if (zip.error || zip.status !== 0) {
88
96
  fs.rmSync(tempDir, { recursive: true, force: true });
97
+ fs.rmSync(archivePath, { force: true });
89
98
  fail(`could not create source archive with zip: ${zip.stderr?.trim() || zip.error?.message || 'unknown error'}`);
90
99
  }
91
100
  return { tempDir, archivePath };
@@ -133,7 +142,7 @@ function main() {
133
142
  const metadata = readFunctionMetadata();
134
143
  const applicationId = (process.env.OCI_FUNCTION_APP_ID || '').trim();
135
144
  if (!applicationId) fail('Terraform output OCI_FUNCTION_APP_ID is missing. Run through "ocdk deploy --code-only".');
136
- const archive = createArchive();
145
+ const archive = createArchive(metadata.functionName);
137
146
  try {
138
147
  const functionId = findFunctionId(applicationId, metadata.functionName);
139
148
  const commonArgs = [
package/bin/ocdk.js CHANGED
@@ -57,6 +57,22 @@ function resolveCodeOnlyFunctionAppId(env) {
57
57
  }
58
58
  }
59
59
 
60
+ function resolveCodeOnlyArchivePath(projectDir, env) {
61
+ let functionName = env.OCI_FUNCTION_NAME?.trim();
62
+ if (!functionName) {
63
+ try {
64
+ const funcYaml = fs.readFileSync(path.join(projectDir, 'func.yaml'), 'utf8');
65
+ const match = funcYaml.match(/^\s*name\s*:\s*(?:["']([^"']*)["']|([^#\r\n]*))/m);
66
+ functionName = (match?.[1] || match?.[2] || '').trim();
67
+ } catch {
68
+ return undefined;
69
+ }
70
+ }
71
+ if (!functionName) return undefined;
72
+ const safeName = functionName.replace(/[^A-Za-z0-9._-]/g, '-');
73
+ return path.join(projectDir, `${safeName || 'function'}.zip`);
74
+ }
75
+
60
76
  const npmRunCommands = ['deploy', 'diff', 'synth', 'destroy', 'list', 'get'];
61
77
 
62
78
  if (!command || command.startsWith('-')) {
@@ -203,6 +219,13 @@ if (command === 'destroy' && codeOnlyEnabled) {
203
219
  shell: false,
204
220
  env,
205
221
  });
222
+ if (infrastructure.status === 0) {
223
+ const archivePath = resolveCodeOnlyArchivePath(projectDir, env);
224
+ if (archivePath && fs.existsSync(archivePath)) {
225
+ fs.rmSync(archivePath, { force: true });
226
+ console.log(`Removed code-only source archive: ${archivePath}`);
227
+ }
228
+ }
206
229
  process.exit(infrastructure.status ?? 1);
207
230
  }
208
231
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikarinneoracle/oci-cdk-code-only-preview",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "OCI CDK stack for OCI Functions, API Gateway, and related infrastructure",
5
5
  "main": "lib/index.js",
6
6
  "bin": {