Migrate telemetry action

This commit is contained in:
Alexandre Bodin 2023-09-05 09:41:13 +02:00
parent b9995616c5
commit a1a2c76ae7
2 changed files with 38 additions and 22 deletions

View File

@ -17,7 +17,7 @@ const readPackageJSON = async (path: string) => {
} }
}; };
const writePackageJSON = async (path: string, file: string, spacing: number) => { const writePackageJSON = async (path: string, file: object, spacing: number) => {
try { try {
await fse.writeJson(path, file, { spaces: spacing }); await fse.writeJson(path, file, { spaces: spacing });
return true; return true;

View File

@ -1,37 +1,51 @@
'use strict'; import { resolve } from 'path';
import { randomUUID } from 'crypto';
import fse from 'fs-extra';
import chalk from 'chalk';
import fetch from 'node-fetch';
import machineID from '../../../../utils/machine-id';
const { resolve } = require('path'); type PackageJson = {
const { randomUUID } = require('crypto'); strapi?: {
const fse = require('fs-extra'); uuid?: string;
const chalk = require('chalk'); telemetryDisabled?: boolean;
const fetch = require('node-fetch'); };
const machineID = require('../../../../utils/machine-id'); };
const readPackageJSON = async (path) => { const readPackageJSON = async (path: string) => {
try { try {
const packageObj = await fse.readJson(path); const packageObj = await fse.readJson(path);
return packageObj; return packageObj;
} catch (err) { } catch (err) {
if (err instanceof Error) {
console.error(`${chalk.red('Error')}: ${err.message}`); console.error(`${chalk.red('Error')}: ${err.message}`);
} else {
throw err;
}
} }
}; };
const writePackageJSON = async (path, file, spacing) => { const writePackageJSON = async (path: string, file: object, spacing: number) => {
try { try {
await fse.writeJson(path, file, { spaces: spacing }); await fse.writeJson(path, file, { spaces: spacing });
return true; return true;
} catch (err) { } catch (err) {
if (err instanceof Error) {
console.error(`${chalk.red('Error')}: ${err.message}`); console.error(`${chalk.red('Error')}: ${err.message}`);
console.log( console.log(
`${chalk.yellow( `${chalk.yellow(
'Warning' 'Warning'
)}: There has been an error, please set "telemetryDisabled": false in the "strapi" object of your package.json manually.` )}: There has been an error, please set "telemetryDisabled": false in the "strapi" object of your package.json manually.`
); );
return false; return false;
} }
throw err;
}
}; };
const generateNewPackageJSON = (packageObj) => { const generateNewPackageJSON = (packageObj: PackageJson) => {
if (!packageObj.strapi) { if (!packageObj.strapi) {
return { return {
...packageObj, ...packageObj,
@ -51,7 +65,7 @@ const generateNewPackageJSON = (packageObj) => {
}; };
}; };
const sendEvent = async (uuid) => { const sendEvent = async (uuid: string) => {
try { try {
const event = 'didOptInTelemetry'; const event = 'didOptInTelemetry';
@ -72,7 +86,7 @@ const sendEvent = async (uuid) => {
} }
}; };
module.exports = async function optInTelemetry() { async function optInTelemetry() {
const packageJSONPath = resolve(process.cwd(), 'package.json'); const packageJSONPath = resolve(process.cwd(), 'package.json');
const exists = await fse.pathExists(packageJSONPath); const exists = await fse.pathExists(packageJSONPath);
@ -101,4 +115,6 @@ module.exports = async function optInTelemetry() {
await sendEvent(updatedPackageJSON.strapi.uuid); await sendEvent(updatedPackageJSON.strapi.uuid);
console.log(`${chalk.green('Successfully opted into and enabled Strapi telemetry')}`); console.log(`${chalk.green('Successfully opted into and enabled Strapi telemetry')}`);
process.exit(0); process.exit(0);
}; }
export default optInTelemetry;