Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env node
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { Command } from "commander";
import { logErrorMessage } from "office-addin-usage-data";
import * as commands from "./commands";
import * as defaults from "./defaults";
/* global process */
const commander = new Command();
commander.name("office-addin-lint");
commander.version(process.env.npm_package_version || "(version not available)");
commander
.command("check")
.option("--files <files>", `Specifies the source files to check. Default: ${defaults.lintFiles}`)
.option("--test", "Use the test lint configuration")
.description(`Check source files against lint rules.`)
.action(commands.lint);
commander
.command("fix")
.option("--files <files>", `Specifies the source files to fix. Default: ${defaults.lintFiles}`)
.option("--test", "Use the test lint configuration")
.description(`Apply fixes to source based on lint rules.`)
.action(commands.lintFix);
commander
.command("prettier")
.option("--files <files>", `Specifies which files to use. Default: ${defaults.lintFiles}`)
.description(`Make the source prettier.`)
.action(commands.prettier);
// if the command is not known, display an error
commander.on("command:*", function () {
logErrorMessage(`The command syntax is not valid.\n`);
process.exitCode = 1;
commander.help();
});
if (process.argv.length > 2) {
commander.parse(process.argv);
} else {
commander.help();
}
+74
View File
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { OptionValues } from "commander";
import { logErrorMessage } from "office-addin-usage-data";
import * as defaults from "./defaults";
import { makeFilesPrettier, performLintCheck, performLintFix } from "./lint";
import { usageDataObject } from "./defaults";
/* global process */
/**
* Determines path to files to run lint against. Priority order follows:
* 1 --files option passed through cli
* 2 lint_files config property in package.json
* 3 default location
* @param command command options which can contain files
*/
function getPathToFiles(options: OptionValues): string {
const pathToFiles: any = options.files
? options.files
: process.env.npm_package_config_lint_files;
return pathToFiles ? pathToFiles : defaults.lintFiles;
}
export async function lint(options: OptionValues) {
try {
const pathToFiles: string = getPathToFiles(options);
const useTestConfig: boolean = options.test;
await performLintCheck(pathToFiles, useTestConfig);
usageDataObject.reportSuccess("lint");
} catch (err: any) {
if (typeof err.status == "number") {
process.exitCode = err.status;
} else {
process.exitCode = defaults.ESLintExitCode.CommandFailed;
usageDataObject.reportException("lint", err);
logErrorMessage(err);
}
}
}
export async function lintFix(options: OptionValues) {
try {
const pathToFiles: string = getPathToFiles(options);
const useTestConfig: boolean = options.test;
await performLintFix(pathToFiles, useTestConfig);
usageDataObject.reportSuccess("lintFix");
} catch (err: any) {
if (typeof err.status == "number") {
process.exitCode = err.status;
} else {
process.exitCode = defaults.ESLintExitCode.CommandFailed;
usageDataObject.reportException("lintFix", err);
logErrorMessage(err);
}
}
}
export async function prettier(options: OptionValues) {
try {
const pathToFiles: string = getPathToFiles(options);
await makeFilesPrettier(pathToFiles);
usageDataObject.reportSuccess("prettier");
} catch (err: any) {
if (typeof err.status == "number") {
process.exitCode = err.status;
} else {
process.exitCode = defaults.PrettierExitCode.CommandFailed;
usageDataObject.reportException("prettier", err);
logErrorMessage(err);
}
}
}
+28
View File
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import {
instrumentationKeyForOfficeAddinCLITools,
OfficeAddinUsageData,
} from "office-addin-usage-data";
export const lintFiles = "src/**/*.{ts,tsx,js,jsx}";
export enum ESLintExitCode {
NoLintErrors = 0,
HasLintError = 1,
CommandFailed = 2,
}
export enum PrettierExitCode {
NoFormattingProblems = 0,
HasFormattingProblem = 1,
CommandFailed = 2,
}
// Usage data defaults
export const usageDataObject: OfficeAddinUsageData = new OfficeAddinUsageData({
projectName: "office-addin-lint",
instrumentationKey: instrumentationKeyForOfficeAddinCLITools,
raisePrompt: false,
});
+101
View File
@@ -0,0 +1,101 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import fs from "fs";
import path from "path";
import { usageDataObject, ESLintExitCode, PrettierExitCode } from "./defaults";
/* global process require __dirname */
const eslintPath = require.resolve("eslint");
const prettierPath = require.resolve("prettier");
const eslintDir = path.parse(eslintPath).dir;
const eslintFilePath = path.resolve(eslintDir, "../bin/eslint.js");
const prettierFilePath = path.resolve(prettierPath, "../bin/prettier.cjs");
const eslintConfigPath = path.resolve(__dirname, "../config/eslint.config.mjs");
const eslintTestConfigPath = path.resolve(__dirname, "../config/eslint.config.test.mjs");
function execCommand(command: string) {
const execSync = require("child_process").execSync;
execSync(command, { stdio: "inherit" });
}
function normalizeFilePath(filePath: string): string {
return filePath.replace(/ /g, "\\ "); // Converting space to '\\'
}
function getEsLintBaseCommand(useTestConfig: boolean = false): string {
const projLintConfig = path.resolve(process.cwd(), "eslint.config.mjs");
const prodConfig = fs.existsSync(projLintConfig) ? projLintConfig : eslintConfigPath;
const configFilePath = useTestConfig ? eslintTestConfigPath : prodConfig;
const eslintBaseCommand: string = `node "${eslintFilePath}" -c "${configFilePath}"`;
return eslintBaseCommand;
}
export function getLintCheckCommand(files: string, useTestConfig: boolean = false): string {
const eslintCommand: string = `${getEsLintBaseCommand(useTestConfig)} "${normalizeFilePath(files)}"`;
return eslintCommand;
}
export function performLintCheck(files: string, useTestConfig: boolean = false) {
try {
const command = getLintCheckCommand(files, useTestConfig);
execCommand(command);
usageDataObject.reportSuccess("performLintCheck()", { exitCode: ESLintExitCode.NoLintErrors });
} catch (err: any) {
if (err.status && err.status == ESLintExitCode.HasLintError) {
usageDataObject.reportExpectedException("performLintCheck()", err, {
exitCode: ESLintExitCode.HasLintError,
});
} else {
usageDataObject.reportException("performLintCheck()", err);
}
throw err;
}
}
export function getLintFixCommand(files: string, useTestConfig: boolean = false): string {
const eslintCommand: string = `${getEsLintBaseCommand(useTestConfig)} --fix ${normalizeFilePath(files)}`;
return eslintCommand;
}
export function performLintFix(files: string, useTestConfig: boolean = false) {
try {
const command = getLintFixCommand(files, useTestConfig);
execCommand(command);
usageDataObject.reportSuccess("performLintFix()", { exitCode: ESLintExitCode.NoLintErrors });
} catch (err: any) {
if (err.status && err.status == ESLintExitCode.HasLintError) {
usageDataObject.reportExpectedException("performLintFix()", err, {
exitCode: ESLintExitCode.HasLintError,
});
} else {
usageDataObject.reportException("performLintFix()", err);
}
throw err;
}
}
export function getPrettierCommand(files: string): string {
const prettierFixCommand: string = `node ${prettierFilePath} --parser typescript --write ${normalizeFilePath(files)}`;
return prettierFixCommand;
}
export function makeFilesPrettier(files: string) {
try {
const command = getPrettierCommand(files);
execCommand(command);
usageDataObject.reportSuccess("makeFilesPrettier()", {
exitCode: PrettierExitCode.NoFormattingProblems,
});
} catch (err: any) {
if (err.status && err.status == PrettierExitCode.HasFormattingProblem) {
usageDataObject.reportExpectedException("makeFilesPrettier()", err, {
exitCode: PrettierExitCode.HasFormattingProblem,
});
} else {
usageDataObject.reportException("makeFilesPrettier()", err);
}
throw err;
}
}
+5
View File
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
export * from "./defaults";
export * from "./lint";