Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tractor to Playwright upgrade script 🚜🔥 #261

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"packages/*",
"plugins/*"
],
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"npmClient": "yarn",
"useWorkspaces": true
}
20 changes: 10 additions & 10 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tractor/cli",
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"description": "CLI for tractor",
"author": "Craig Spence <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -35,18 +35,18 @@
"build": "src/**/*.js"
},
"dependencies": {
"@tractor/core": "^1.9.4-alpha.4",
"@tractor/core": "^1.9.4-tractor-to-playwright.0",
"commander": "^2.13.0"
},
"devDependencies": {
"@tractor/config-loader": "^1.9.4-alpha.4",
"@tractor/dependency-injection": "^1.9.4-alpha.4",
"@tractor/error-handler": "^1.9.4-alpha.4",
"@tractor/file-structure": "^1.9.4-alpha.4",
"@tractor/logger": "^1.9.4-alpha.4",
"@tractor/server": "^1.9.4-alpha.4",
"@tractor/tractor": "^1.9.4-alpha.4",
"@tractor/unit-test": "^1.9.4-alpha.4"
"@tractor/config-loader": "^1.9.4-tractor-to-playwright.0",
"@tractor/dependency-injection": "^1.9.4-tractor-to-playwright.0",
"@tractor/error-handler": "^1.9.4-tractor-to-playwright.0",
"@tractor/file-structure": "^1.9.4-tractor-to-playwright.0",
"@tractor/logger": "^1.9.4-tractor-to-playwright.0",
"@tractor/server": "^1.9.4-tractor-to-playwright.0",
"@tractor/tractor": "^1.9.4-tractor-to-playwright.0",
"@tractor/unit-test": "^1.9.4-tractor-to-playwright.0"
},
"gitHead": "aac58387d7addbeceb2683c730fd7801922f7426"
}
13 changes: 7 additions & 6 deletions packages/cli/src/upgrade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { inject } from '@tractor/dependency-injection';
import { info } from '@tractor/logger';
import { Tractor } from '@tractor/tractor';

export const upgrade = inject(async (tractor: Tractor): Promise<Array<void>> => Promise.all(
tractor.plugins.map(plugin => {
info(`Upgrading tractor-plugin-${plugin.name} files...`);
export const upgrade = inject(async (tractor: Tractor): Promise<void> => tractor.plugins.reduce(
async (p, plugin) => {
await p;
info(`Upgrading @tractor-plugin/${plugin.name} files...`);
// HACK:
// Here's another reason to kill the custom DI stuff:
// tslint:disable-next-line:no-unbound-method
return tractor.call(plugin.upgrade);
})
) , 'tractor');
await tractor.call(plugin.upgrade);
}, Promise.resolve())
, 'tractor');
8 changes: 4 additions & 4 deletions packages/config-loader/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tractor/config-loader",
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"description": "Config loader for tractor",
"author": "Craig Spence <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -37,9 +37,9 @@
"@tractor/core": "^1.0.0"
},
"devDependencies": {
"@tractor/error-handler": "^1.9.4-alpha.4",
"@tractor/logger": "^1.9.4-alpha.4",
"@tractor/unit-test": "^1.9.4-alpha.4"
"@tractor/error-handler": "^1.9.4-tractor-to-playwright.0",
"@tractor/logger": "^1.9.4-tractor-to-playwright.0",
"@tractor/unit-test": "^1.9.4-tractor-to-playwright.0"
},
"gitHead": "aac58387d7addbeceb2683c730fd7801922f7426"
}
24 changes: 12 additions & 12 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tractor/core",
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"description": "Core module for tractor",
"author": "Craig Spence <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -32,19 +32,19 @@
"build": "src/**/*.js"
},
"dependencies": {
"@tractor/config-loader": "^1.9.4-alpha.4",
"@tractor/dependency-injection": "^1.9.4-alpha.4",
"@tractor/error-handler": "^1.9.4-alpha.4",
"@tractor/file-javascript": "^1.9.4-alpha.4",
"@tractor/file-structure": "^1.9.4-alpha.4",
"@tractor/logger": "^1.9.4-alpha.4",
"@tractor/plugin-loader": "^1.9.4-alpha.4",
"@tractor/server": "^1.9.4-alpha.4",
"@tractor/tractor": "^1.9.4-alpha.4",
"@tractor/ui": "^1.9.4-alpha.4"
"@tractor/config-loader": "^1.9.4-tractor-to-playwright.0",
"@tractor/dependency-injection": "^1.9.4-tractor-to-playwright.0",
"@tractor/error-handler": "^1.9.4-tractor-to-playwright.0",
"@tractor/file-javascript": "^1.9.4-tractor-to-playwright.0",
"@tractor/file-structure": "^1.9.4-tractor-to-playwright.0",
"@tractor/logger": "^1.9.4-tractor-to-playwright.0",
"@tractor/plugin-loader": "^1.9.4-tractor-to-playwright.0",
"@tractor/server": "^1.9.4-tractor-to-playwright.0",
"@tractor/tractor": "^1.9.4-tractor-to-playwright.0",
"@tractor/ui": "^1.9.4-tractor-to-playwright.0"
},
"devDependencies": {
"@tractor/unit-test": "^1.9.4-alpha.4"
"@tractor/unit-test": "^1.9.4-tractor-to-playwright.0"
},
"gitHead": "aac58387d7addbeceb2683c730fd7801922f7426"
}
4 changes: 2 additions & 2 deletions packages/dependency-injection/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tractor/dependency-injection",
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"description": "DI container for tractor",
"author": "Craig Spence <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -37,7 +37,7 @@
"@tractor/core": "^1.0.0"
},
"devDependencies": {
"@tractor/error-handler": "^1.9.4-alpha.4"
"@tractor/error-handler": "^1.9.4-tractor-to-playwright.0"
},
"gitHead": "aac58387d7addbeceb2683c730fd7801922f7426"
}
6 changes: 3 additions & 3 deletions packages/error-handler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tractor/error-handler",
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"description": "JavaScript file handler for tractor",
"author": "Craig Spence <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -40,8 +40,8 @@
"@tractor/core": "^1.0.0"
},
"devDependencies": {
"@tractor/logger": "^1.9.4-alpha.4",
"@tractor/unit-test": "^1.9.4-alpha.4",
"@tractor/logger": "^1.9.4-tractor-to-playwright.0",
"@tractor/unit-test": "^1.9.4-tractor-to-playwright.0",
"@types/express": "^4.16.1"
},
"gitHead": "aac58387d7addbeceb2683c730fd7801922f7426"
Expand Down
10 changes: 6 additions & 4 deletions packages/file-javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tractor/file-javascript",
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"description": "JavaScript file handler for tractor",
"author": "Craig Spence <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -37,15 +37,17 @@
"dependencies": {
"escodegen": "^1.9.0",
"esprima": "^4.0.1",
"esquery": "^1.0.1"
"esquery": "^1.0.1",
"prettier": "^2.3.2"
},
"peerDependencies": {
"@tractor/core": "^1.0.0"
},
"devDependencies": {
"@tractor/file-structure": "^1.9.4-alpha.4",
"@tractor/file-structure": "^1.9.4-tractor-to-playwright.0",
"@types/escodegen": "^0.0.6",
"@types/esprima": "^4.0.2"
"@types/esprima": "^4.0.2",
"@types/prettier": "^2.3.2"
},
"gitHead": "aac58387d7addbeceb2683c730fd7801922f7426"
}
3 changes: 3 additions & 0 deletions packages/file-javascript/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from './javascript-file-metadata';
export * from './javascript-file-refactorer';
export * from './javascript-file';
export * from './typescript-file-metadata';
export * from './typescript-file-refactorer';
export * from './typescript-file';
8 changes: 5 additions & 3 deletions packages/file-javascript/src/javascript-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export class JavaScriptFile <MetadataType extends JavaScriptFileMetaType = JavaS

await refactor;
const change = JAVASCRIPT_FILE_REFACTORER[type];
if (!change) {
return;
if (change) {
const result = await change(this, data);
if (result === null) {
return;
}
}
await change(this, data);
await this.save(this.ast as Program);
}

Expand Down
17 changes: 17 additions & 0 deletions packages/file-javascript/src/typescript-file-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Dependencies:
import { FileMetadata } from '@tractor/file-structure';
import { SourceFile } from 'typescript';

export type TypeScriptFileMetaItem = {
name?: string;
version?: string;
};

export type TypeScriptFileMetaType = TypeScriptFileMetaItem & {
[key: string]: Array<TypeScriptFileMetaItem>;
};

export type TypeScriptFileMetadata<MetaType extends TypeScriptFileMetaType = TypeScriptFileMetaType> = FileMetadata & {
ast?: SourceFile;
meta: MetaType | null;
};
5 changes: 5 additions & 0 deletions packages/file-javascript/src/typescript-file-refactorer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Dependencies:
import { Refactorer } from '@tractor/file-structure';
import { TypeScriptFile } from './typescript-file';

export const TYPESCRIPT_FILE_REFACTORER: Refactorer<TypeScriptFile> = {};
131 changes: 131 additions & 0 deletions packages/file-javascript/src/typescript-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Constants:
const REQUEST_ERROR = 400;
const IMPORT_QUERY = 'ImportDeclaration > StringLiteral';

// Dependencies:
import { File, RefactorData } from '@tractor/file-structure';
import { tsquery } from '@phenomnomnominal/tsquery';
import { createPrinter, SourceFile, StringLiteral } from 'typescript';
import * as path from 'path';
import { format } from 'prettier';
import { TYPESCRIPT_FILE_REFACTORER } from './typescript-file-refactorer';
import { TypeScriptFileMetadata, TypeScriptFileMetaType } from './typescript-file-metadata';

// Errors:
import { TractorError } from '@tractor/error-handler';

export class TypeScriptFile<MetadataType extends TypeScriptFileMetaType = TypeScriptFileMetaType> extends File {
public ast?: SourceFile;
public initialised = false;

public async meta (): Promise<MetadataType | null> {
if (!this.ast) {
await this.read();
}
return this._getMetaSync();
}

public async read (): Promise<string> {
const read = super.read();

try {
const content = await read;
this._setAST(content);
this._getReferences();
return this.content!;
} catch {
throw new TractorError(`Parsing "${this.path}" failed.`, REQUEST_ERROR);
}
}

public async refactor (type: string, data?: RefactorData): Promise<void> {
const refactor = super.refactor(type, data);

await refactor;
const change = TYPESCRIPT_FILE_REFACTORER[type];
if (change) {
const result = await change(this, data);
if (result === null) {
return;
}
}
await this.save(this.ast as SourceFile);
}

public async save (typescript: string | Buffer | SourceFile): Promise<string> {
let toSave: string | Buffer;
if (typeof typescript !== 'string' && !Buffer.isBuffer(typescript)) {
const printer = createPrinter();
toSave = printer.printFile(typescript);
toSave = format(toSave.toString(), { printWidth: 200, singleQuote: true, parser: 'typescript' });
} else {
toSave = typescript;
}

const save = super.save(toSave);

try {
const content = await save;
this._setAST(content);
this._getReferences();
return this.content as string;
} catch {
throw new TractorError(`Saving "${this.path}" failed.`, REQUEST_ERROR);
}
}

public serialise (): TypeScriptFileMetadata<MetadataType> {
const serialised = super.serialise() as TypeScriptFileMetadata<MetadataType>;

serialised.ast = this.ast;
return serialised;
}

public toJSON (): TypeScriptFileMetadata<MetadataType> {
const json = super.toJSON() as TypeScriptFileMetadata<MetadataType>;
json.meta = this._getMetaSync();
return json;
}

private _getMetaSync (): MetadataType | null {
try {
return {} as MetadataType;
} catch {
return null;
}
}

private _getReferences (): void {
if (this.initialised) {
this.fileStructure.referenceManager.clearReferences(this.path);
}

tsquery(this.ast!, IMPORT_QUERY).forEach(importPath => {
const directoryPath = path.dirname(this.path);
let referencePath = path.resolve(
directoryPath,
(importPath as StringLiteral).text
);
if (referencePath.endsWith('.page')) {
referencePath = referencePath.replace('.page', '.po.js');
}
if (!path.extname(referencePath)) {
referencePath = `${referencePath}${TypeScriptFile.prototype.extension}`;
}
const reference =
this.fileStructure.referenceManager.getReference(referencePath);
if (reference) {
this.addReference(reference);
}
});

this.initialised = true;
}

private _setAST (content: string): string {
this.ast = tsquery.ast(content);
return content;
}
}

TypeScriptFile.prototype.extension = '.ts';
10 changes: 5 additions & 5 deletions packages/file-structure/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tractor/file-structure",
"version": "1.9.4-alpha.4",
"version": "1.9.4-tractor-to-playwright.0",
"description": "File structure handler for tractor",
"author": "Craig Spence <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -45,10 +45,10 @@
"@tractor/core": "^1.0.0"
},
"devDependencies": {
"@tractor/dependency-injection": "^1.9.4-alpha.4",
"@tractor/error-handler": "^1.9.4-alpha.4",
"@tractor/logger": "^1.9.4-alpha.4",
"@tractor/unit-test": "^1.9.4-alpha.4",
"@tractor/dependency-injection": "^1.9.4-tractor-to-playwright.0",
"@tractor/error-handler": "^1.9.4-tractor-to-playwright.0",
"@tractor/logger": "^1.9.4-tractor-to-playwright.0",
"@tractor/unit-test": "^1.9.4-tractor-to-playwright.0",
"@types/body-parser": "^1.17.0",
"@types/graceful-fs": "^4.1.3",
"@types/node-fetch": "^2.1.6",
Expand Down
Loading