Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

fix: INT-2334 ensured form-data has buffers when sending requests to CS #177

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "intuita",
"version": "0.5.3",
"version": "0.6.0",
"description": "A codemod engine for Node.js libraries (jscodeshift, ts-morph, etc.)",
"type": "module",
"exports": null,
Expand Down Expand Up @@ -82,7 +82,7 @@
},
"packageManager": "[email protected]",
"scripts": {
"build": "esbuild ./src/index.ts --define:__INTUITA_CLI_VERSION__=\\\"0.5.3\\\" --bundle --platform=node --target=node16 --minify --format=cjs --legal-comments=inline --outfile=./dist/index.cjs",
"build": "esbuild ./src/index.ts --define:__INTUITA_CLI_VERSION__=\\\"0.6.0\\\" --bundle --platform=node --target=node16 --minify --format=cjs --legal-comments=inline --outfile=./dist/index.cjs",
"lint:eslint": "eslint src --fix --ext ts",
"lint:prettier": "prettier --write .",
"package": "pkg --compress GZip .",
Expand Down
14 changes: 14 additions & 0 deletions src/apis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Axios from 'axios';
import { type Input, parse, object, string, nullable } from 'valibot';
import type FormData from 'form-data';

const X_INTUITA_ACCESS_TOKEN = 'X-Intuita-Access-Token'.toLocaleLowerCase();

Expand All @@ -25,3 +26,16 @@ export const validateAccessToken = async (

return parse(dataSchema, response.data);
};

export const publish = async (
accessToken: string,
formData: FormData,
): Promise<void> => {
await Axios.post('https://telemetry.intuita.io/publish', formData, {
headers: {
[X_INTUITA_ACCESS_TOKEN]: accessToken,
'Content-Type': 'multipart/form-data',
},
timeout: 10000,
});
};
28 changes: 19 additions & 9 deletions src/handlePublishCliCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import * as fs from 'fs';
import type { PrinterBlueprint } from './printer.js';
import { homedir } from 'node:os';
import { join } from 'node:path';
import { validateAccessToken } from './apis.js';
import Axios from 'axios';

import { object, string, parse } from 'valibot';
import { publish, validateAccessToken } from './apis.js';
import { CodemodDownloader } from './downloadCodemod.js';
import FormData from 'form-data';

const packageJsonSchema = object({
main: string(),
Expand Down Expand Up @@ -81,17 +80,28 @@ export const handlePublishCliCommand = async (
}

const formData = new FormData();
formData.append('package.json', packageJsonData);
formData.append('index.cjs', indexCjsData);
formData.append('config.json', configJsonData);
formData.append('index.cjs', Buffer.from(indexCjsData));
formData.append('config.json', Buffer.from(configJsonData));

if (descriptionMdData) {
formData.append('description.md', descriptionMdData);
}

await Axios.post('https://telemetry.intuita.io/publish', formData, {
timeout: 10000,
});
try {
await publish(token, formData);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);

printer.printConsoleMessage(
'error',
`Could not publish the "${pkg.name}" package: ${message}`,
);
}

printer.printConsoleMessage(
'info',
`Published the "${pkg.name}" package successfully`,
);

try {
await codemodDownloader.download(pkg.name);
Expand Down