Fix tests and build

Remove unused code and dependencies
This commit is contained in:
Dolan Miu
2023-06-08 13:30:31 +01:00
parent a0a88412ff
commit 09ab91eeef
19 changed files with 22322 additions and 26082 deletions

View File

@ -4,13 +4,7 @@
"version": "0.2",
// language - current active spelling language
"language": "en_US",
"dictionaries": [
"en_US",
"typescript",
"softwareTerms",
"fonts",
"npm"
],
"dictionaries": ["en_US", "typescript", "softwareTerms", "fonts", "npm"],
// words - list of words to be always considered correct
"words": [
"Abjad",
@ -24,12 +18,15 @@
"execa",
"falsey",
"fflate",
"iife",
"Initializable",
"iroha",
"jsonify",
"jszip",
"NUMPAGES",
"odttf",
"ondata",
"onfile",
"ooxml",
"panose",
"rels",
@ -59,15 +56,10 @@
"/<element name=\"[a-z]+\"/gi",
"/<attribute name=\"[a-z]+\"/gi"
],
"ignorePaths": [
"package.json",
"docs/api"
],
"ignorePaths": ["package.json", "docs/api", "*.docx", "build"],
"allowCompoundWords": true,
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// For example "hte" should be "the"
"flagWords": [
"hte"
]
"flagWords": ["hte"]
}

View File

@ -5,32 +5,3 @@ updates:
schedule:
interval: daily
open-pull-requests-limit: 10
ignore:
- dependency-name: "@types/node"
versions:
- 14.14.22
- 14.14.24
- 14.14.25
- 14.14.26
- 14.14.28
- 14.14.29
- 14.14.30
- 15.0.0
- dependency-name: ts-loader
versions:
- 9.1.0
- dependency-name: awesome-typescript-loader
versions:
- 5.2.1
- dependency-name: chai
versions:
- 4.2.0
- 4.3.0
- 4.3.1
- 4.3.3
- dependency-name: replace-in-file
versions:
- 6.1.0
- dependency-name: "@types/chai"
versions:
- 4.2.14

View File

@ -1,13 +0,0 @@
{
"diff": true,
"spec": "src/**/*.ts",
"extension": ["ts"],
"package": "./package.json",
"reporter": "spec",
"slow": "75",
"timeout": "2000",
"ui": "bdd",
"watch-files": ["src/**/*.ts"],
"watch-ignore": ["build", "demo"],
"require": ["mocha.env", "ts-node/register", "tsconfig-paths/register"]
}

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="../build/index.js"></script>
<script src="../build/index.umd.cjs"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
</head>

View File

@ -1,34 +1,53 @@
/* eslint-disable no-console */
import fs from "fs";
import prompt, { Schema } from "prompt";
import path from "path";
import inquirer from "inquirer";
import { $ } from "execa";
console.log("What demo do you wish to run? (Enter a number)");
const schema: Schema = {
properties: {
demoNumber: {
pattern: /^[0-9]+$/,
message: "Please enter a number.",
required: true,
},
},
export type Answers = {
type: "list" | "number";
demoNumber?: number;
demoFile?: number;
};
prompt.start();
const dir = "./demo";
const fileNames = fs.readdirSync(dir);
prompt.get(schema, async (_, result) => {
const demoNumber = result.demoNumber as string;
const files = fs.readdirSync("./demo").filter((fn) => fn.startsWith(demoNumber));
const keys = fileNames.map((f) => path.parse(f).name);
if (files.length === 0) {
console.error(`demo number ${demoNumber} does not exist`);
return;
}
const answers = await inquirer.prompt<Answers>([
{
type: "list",
name: "type",
message: "Select demo from a list or via number",
choices: ["list", "number"],
},
{
type: "list",
name: "demoFile",
message: "What demo do you wish to run?",
choices: keys,
filter: (input) => parseInt(input.split("-")[0]),
when: (answers) => answers.type === "list",
},
{
type: "number",
name: "demoNumber",
message: "What demo do you wish to run? (Enter a number)",
default: 1,
when: (answers) => answers.type === "number",
},
]);
const filePath = `./demo/${files[0]}`;
const demoNumber = answers.demoNumber ?? answers.demoFile ?? 1;
const files = fs.readdirSync(dir).filter((fn) => fn.startsWith(demoNumber.toString()));
if (files.length === 0) {
console.error(`demo number ${demoNumber} does not exist`);
} else {
const filePath = path.join(dir, files[0]);
console.log(`Running demo ${demoNumber}: ${files[0]}`);
await $`ts-node --project demo/tsconfig.json ${filePath}`;
console.log("Successfully created document!");
});
}

View File

@ -191,9 +191,13 @@ Please write a test of every file you make and suffix it with `.spec.ts`.
Here is a template of a test:
```ts
import { assert } from "chai";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
describe("ClassName", () => {
afterEach(() => {
// TODO
});
beforeEach(() => {
// TODO
});

View File

@ -1 +0,0 @@
process.env.TS_NODE_PROJECT = 'tsconfig.spec.json'

47909
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,133 +1,117 @@
{
"name": "docx",
"version": "8.0.4",
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
"type": "module",
"main": "build/index.umd.cjs",
"module": "./build/index.js",
"types": "./build/index.d.ts",
"exports": {
".": {
"browser": {
"default": "./build/index.umd.cjs"
},
"require": "./build/index.cjs",
"types": "./build/index.d.ts",
"import": "./build/index.js",
"default": "./build/index.js"
"name": "docx",
"version": "8.0.4",
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
"type": "module",
"main": "build/index.umd.cjs",
"module": "./build/index.js",
"types": "./build/index.d.ts",
"exports": {
".": {
"browser": {
"default": "./build/index.umd.cjs"
},
"require": "./build/index.cjs",
"types": "./build/index.d.ts",
"import": "./build/index.js",
"default": "./build/index.js"
}
},
"files": [
"build"
],
"scripts": {
"build": "tsc && vite build",
"test": "vitest run",
"test.coverage": "nyc npm test",
"test.watch": "vitest",
"prepublishOnly": "npm run build --omit=dev",
"lint": "eslint --ext .ts src",
"predemo": "npm run build",
"demo": "ts-node --project demo/tsconfig.json ./demo/index.ts",
"typedoc": "typedoc src/index.ts --tsconfig tsconfig.typedoc.json",
"style": "prettier -l \"{src,scripts,demo}/**/*.{ts,html}\"",
"style.fix": "npm run style -- --write",
"cspell": "cspell \"{src,demo,docs,scripts}/**/*.{ts,scss,html,md}\" && cspell \"./*.*\"",
"serve.docs": "cd docs && docsify serve",
"extract": "ts-node scripts/extract-document.ts",
"ts-node": "ts-node --project demo/tsconfig.json"
},
"pre-commit": [
"style",
"lint"
],
"repository": {
"type": "git",
"url": "git+https://github.com/dolanmiu/docx.git"
},
"keywords": [
"docx",
"office",
"word",
"generate",
"creator",
"create",
"document",
"doc",
"officegen",
"clippy"
],
"dependencies": {
"@types/node": "^18.0.0",
"fflate": "^0.8.0",
"jszip": "^3.10.1",
"nanoid": "^3.3.4",
"xml": "^1.0.1",
"xml-js": "^1.6.8"
},
"author": "Dolan Miu",
"license": "MIT",
"bugs": {
"url": "https://github.com/dolanmiu/docx/issues"
},
"homepage": "https://docx.js.org",
"devDependencies": {
"@types/chai": "^4.2.15",
"@types/chai-as-promised": "^7.1.5",
"@types/inquirer": "^9.0.3",
"@types/prompt": "^1.1.1",
"@types/unzipper": "^0.10.4",
"@types/xml": "^1.0.8",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"cspell": "^6.2.2",
"docsify-cli": "^4.3.0",
"eslint": "^8.23.0",
"eslint-plugin-functional": "^5.0.8",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsdoc": "^40.0.0",
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-unicorn": "^46.0.0",
"execa": "^7.1.1",
"glob": "^9.3.0",
"inquirer": "^9.2.7",
"jsdom": "^22.1.0",
"nyc": "^15.1.0",
"pre-commit": "^1.2.2",
"prettier": "^2.3.1",
"process": "^0.11.10",
"prompt": "^1.0.0",
"ts-node": "^10.2.1",
"tsconfig-paths": "^4.0.0",
"typedoc": "^0.23.2",
"typescript": "5.0.3",
"unzipper": "^0.10.11",
"vite": "^4.3.2",
"vite-plugin-dts": "^2.3.0",
"vite-plugin-node-polyfills": "^0.8.2",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.31.4"
},
"engines": {
"node": ">=10"
}
},
"files": [
"build"
],
"scripts": {
"build": "tsc && vite build",
"test": "vitest",
"test.coverage": "nyc npm test",
"test.watch": "npm test -- --watch",
"prepublishOnly": "npm run build --omit=dev",
"lint": "eslint --ext .ts src",
"demo": "ts-node --project demo/tsconfig.json ./demo/index.ts",
"typedoc": "rimraf ./build && typedoc src/index.ts --tsconfig tsconfig.typedoc.json",
"style": "prettier -l \"{src,scripts,demo}/**/*.{ts,html}\"",
"style.fix": "npm run style -- --write",
"cspell": "cspell \"{src,demo,docs,scripts}/**/*.{ts,scss,html,md}\" && cspell \"./*.*\"",
"e2e": "ts-node scripts/e2e.ts",
"serve.docs": "cd docs && docsify serve",
"extract": "ts-node --project tsconfig.spec.json scripts/extract-document.ts",
"ts-node": "ts-node --project demo/tsconfig.json"
},
"pre-commit": [
"style",
"lint"
],
"repository": {
"type": "git",
"url": "git+https://github.com/dolanmiu/docx.git"
},
"keywords": [
"docx",
"office",
"word",
"generate",
"creator",
"create",
"document",
"doc",
"officegen",
"clippy"
],
"dependencies": {
"@types/node": "^18.0.0",
"fflate": "^0.8.0",
"jszip": "3.2.0",
"nanoid": "^3.3.4",
"xml": "^1.0.1",
"xml-js": "^1.6.8"
},
"author": "Dolan Miu",
"license": "MIT",
"bugs": {
"url": "https://github.com/dolanmiu/docx/issues"
},
"homepage": "https://docx.js.org",
"devDependencies": {
"@types/chai": "^4.2.15",
"@types/chai-as-promised": "^7.1.5",
"@types/mocha": "^10.0.0",
"@types/prompt": "^1.1.1",
"@types/request-promise": "^4.1.42",
"@types/shelljs": "^0.8.11",
"@types/sinon": "^10.0.0",
"@types/unzipper": "^0.10.4",
"@types/webpack": "^5.0.0",
"@types/xml": "^1.0.8",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"buffer": "^6.0.3",
"chai": "^4.3.6",
"chai-as-promised": "^7.1.1",
"cspell": "^6.2.2",
"docsify-cli": "^4.3.0",
"eslint": "^8.23.0",
"eslint-plugin-functional": "^5.0.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsdoc": "^40.0.0",
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-prefer-arrow": "^1.2.3",
"eslint-plugin-unicorn": "^46.0.0",
"execa": "^7.1.1",
"glob": "^9.3.0",
"jsdom": "^22.1.0",
"jszip": "^3.1.5",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"pre-commit": "^1.2.2",
"prettier": "^2.3.1",
"process": "^0.11.10",
"prompt": "^1.0.0",
"replace-in-file": "^6.2.0",
"request": "^2.88.0",
"request-promise": "^4.2.2",
"rimraf": "^4.0.4",
"sinon": "^15.0.0",
"stream-browserify": "^3.0.0",
"ts-loader": "^9.0.0",
"ts-node": "^10.2.1",
"tsconfig-paths": "^4.0.0",
"tsconfig-paths-webpack-plugin": "^4.0.0",
"typedoc": "^0.23.2",
"typescript": "5.0.3",
"unzipper": "^0.10.11",
"vite": "^4.3.2",
"vite-plugin-dts": "^2.3.0",
"vite-plugin-node-polyfills": "^0.8.2",
"vite-tsconfig-paths": "^4.2.0",
"vitest": "^0.31.4",
"webpack": "^5.28.0",
"webpack-cli": "^5.0.0"
},
"engines": {
"node": ">=10"
}
}

View File

@ -1,30 +0,0 @@
// tslint:disable:no-console
import * as fs from "fs";
import * as request from "request-promise";
async function e2e(filePath: string): Promise<void> {
console.log(`Running e2e for: ${filePath}`);
if (!fs.existsSync(filePath)) {
console.error("File not found");
throw Error("File not found");
}
const result = await request.post({
url: "https://wt-9017166451e5dc00461b648d19f5e8da-0.sandbox.auth0-extend.com/docx-validator",
formData: {
document: fs.createReadStream(filePath),
},
});
return result;
}
e2e(process.argv[2])
.then(() => {
console.log("Success! Document is valid");
})
.catch(() => {
console.log("Error! Validation failed");
process.exit(1);
});

View File

@ -1,18 +0,0 @@
import * as glob from "glob";
import { replaceInFile } from "replace-in-file";
const files = glob.sync("build/**/*.d.ts");
for (const file of files) {
replaceInFile({
files: file,
from: /"@[a-z/-]*"/gi,
to: (match) => {
const matchSlug = match.replace(/['"]+/g, "").replace(/[@]+/g, "").trim();
const levelCount = file.split(/[\/\\]/).length - 2;
const backLevels = Array(levelCount).fill("../").join("");
return `"${backLevels}${matchSlug}"`;
},
});
}

View File

@ -1,5 +1,4 @@
import { assert, beforeEach, describe, expect, it } from "vitest";
import * as sinon from "sinon";
import { assert, beforeEach, describe, expect, it, vi } from "vitest";
import { Formatter } from "@export/formatter";
import { CoreProperties } from "@file/core-properties";
@ -109,10 +108,10 @@ describe("Formatter", () => {
it("should call the prep method only once", () => {
const paragraph = new Paragraph("");
const spy = sinon.spy(paragraph, "prepForXml");
const spy = vi.spyOn(paragraph, "prepForXml");
formatter.format(paragraph);
expect(spy.calledOnce).to.equal(true);
expect(spy).toBeCalledTimes(1);
});
});
});

View File

@ -1,6 +1,5 @@
/* tslint:disable:typedef space-before-function-paren */
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import * as sinon from "sinon";
import * as fflate from "fflate";
import { File } from "@file/file";
@ -140,10 +139,10 @@ describe("Compiler", () => {
});
// tslint:disable-next-line: no-string-literal
const spy = sinon.spy(compiler["formatter"], "format");
const spy = vi.spyOn(compiler["formatter"], "format");
compiler.compile(file);
expect(spy.callCount).to.equal(13);
expect(spy).toBeCalledTimes(13);
});
it("should work with media datas", () => {
@ -179,8 +178,7 @@ describe("Compiler", () => {
],
});
// tslint:disable-next-line: no-string-literal
sinon.stub(compiler["imageReplacer"], "getMediaData").returns([
vi.spyOn(compiler["imageReplacer"], "getMediaData").mockReturnValue([
{
stream: Buffer.from(""),
fileName: "test",

View File

@ -1,5 +1,4 @@
import { describe, expect, it } from "vitest";
import * as sinon from "sinon";
import { describe, expect, it, vi } from "vitest";
import { FooterWrapper } from "./footer-wrapper";
import { Media } from "./media";
@ -10,15 +9,15 @@ describe("FooterWrapper", () => {
describe("#add", () => {
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.View, "add");
const spy = vi.spyOn(file.View, "add");
file.add(new Paragraph({}));
expect(spy.called).to.equal(true);
expect(spy).toBeCalled();
});
it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.View, "add");
const spy = vi.spyOn(file.View, "add");
file.add(
new Table({
rows: [
@ -33,18 +32,18 @@ describe("FooterWrapper", () => {
}),
);
expect(spy.called).to.equal(true);
expect(spy).toBeCalled();
});
});
describe("#addChildElement", () => {
it("should call the underlying footer's addChildElement", () => {
const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.View, "addChildElement");
const spy = vi.spyOn(file.View, "addChildElement");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
file.addChildElement({} as any);
expect(spy.called).to.equal(true);
expect(spy).toBeCalled();
});
});

View File

@ -1,5 +1,4 @@
import { describe, expect, it } from "vitest";
import * as sinon from "sinon";
import { describe, expect, it, vi } from "vitest";
import { HeaderWrapper } from "./header-wrapper";
import { Media } from "./media";
@ -10,15 +9,15 @@ describe("HeaderWrapper", () => {
describe("#add", () => {
it("should call the underlying header's addChildElement for Paragraph", () => {
const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.View, "add");
const spy = vi.spyOn(wrapper.View, "add");
wrapper.add(new Paragraph({}));
expect(spy.called).to.equal(true);
expect(spy).toBeCalled();
});
it("should call the underlying header's addChildElement for Table", () => {
const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.View, "add");
const spy = vi.spyOn(wrapper.View, "add");
wrapper.add(
new Table({
rows: [
@ -33,18 +32,18 @@ describe("HeaderWrapper", () => {
}),
);
expect(spy.called).to.equal(true);
expect(spy).toBeCalled();
});
});
describe("#addChildElement", () => {
it("should call the underlying header's addChildElement", () => {
const file = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(file.View, "addChildElement");
const spy = vi.spyOn(file.View, "addChildElement");
// eslint-disable-next-line @typescript-eslint/no-explicit-any
file.addChildElement({} as any);
expect(spy.called).to.equal(true);
expect(spy).toBeCalled();
});
});

View File

@ -1,5 +1,4 @@
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import * as sinon from "sinon";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Formatter } from "@export/formatter";
@ -43,15 +42,15 @@ describe("CommentReference", () => {
});
describe("Comment", () => {
let clock: sinon.SinonFakeTimers;
beforeEach(() => {
const now = new Date("1999-01-01T00:00:00.000Z");
clock = sinon.useFakeTimers(now.getTime());
vi.useFakeTimers({
now: now.getTime(),
});
});
afterEach(() => {
clock.restore();
vi.restoreAllMocks();
});
describe("#constructor()", () => {

View File

@ -2,8 +2,7 @@ import { IViewWrapper } from "@file/document-wrapper";
import { File } from "@file/file";
import { Paragraph, TextRun } from "@file/paragraph";
import { IContext } from "@file/xml-components";
import { describe, expect, it } from "vitest";
import * as sinon from "sinon";
import { describe, expect, it, vi } from "vitest";
import { PatchType } from "./from-docx";
@ -63,7 +62,8 @@ describe("replacer", () => {
},
"hello",
[],
sinon.mock() as unknown as IContext,
// eslint-disable-next-line functional/prefer-readonly-type
vi.fn<[], IContext>()(),
);
expect(output).to.deep.equal({

View File

@ -1,7 +0,0 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"target": "es6"
}
}

View File

@ -5,7 +5,7 @@
},
"typedocOptions": {
"out": "docs/api",
"exclude": "test",
"exclude": ["test", "build"],
"theme": "default",
"excludePrivate": true,
"excludeProtected": true,