Skip to content

Commit 93d47a5

Browse files
committed
feat(command): convert CommonJs to Typescript
Convert CommonJS file from Explorer tree to typescript
1 parent 8806497 commit 93d47a5

7 files changed

Lines changed: 1216 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +0,0 @@
1-
# Changelog
2-
3-
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4-
5-
### [0.0.4](https://github.com/CodersAKL/react-toolkit/compare/v0.0.2...v0.0.4) (2020-02-14)
6-
7-
### [0.0.3](https://github.com/CodersAKL/react-toolkit/compare/v0.0.2...v0.0.3) (2020-02-13)
8-
9-
### 0.0.2 (2020-02-12)

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
# react-toolkit
22

3-
Refactor React Native source code extracting inline styles from the VSCode menu
3+
This is VsCode plugin suppose to help developers convert their project to typescript.
44

5-
![demo](assets/demo.gif)
5+
Any suggestion are more then welcome
66

7-
![demo](assets/demo2.gif)
7+
## Refactor React Native source code extracting inline styles from the VSCode menu
8+
9+
![demo1](assets/demo.gif)
10+
11+
## Convert ReactJs to ReactTypescript
12+
13+
![demo2](assets/demo2.gif)
14+
15+
## Convert CommonJs to Typescript
16+
17+
![convert CommonJs to Typescript](assets/demo3.gif)

assets/demo3.gif

274 KB
Loading

package.json

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
"vscode": "^1.42.0"
2323
},
2424
"activationEvents": [
25-
"*",
26-
"onLanguage:jsx",
25+
"onLanguage:js",
2726
"onLanguage:tsx",
2827
"onLanguage:jsx",
2928
"onLanguage:javascript",
@@ -41,6 +40,10 @@
4140
{
4241
"command": "react-toolkit.readFile",
4342
"title": "Convert to Typescript"
43+
},
44+
{
45+
"command": "react-toolkit.convertCjsToEsm",
46+
"title": "Convert CommonJS to Typescript"
4447
}
4548
],
4649
"menus": {
@@ -55,18 +58,29 @@
5558
{
5659
"command": "react-toolkit.readFile",
5760
"group": "1_modification@1"
61+
},
62+
{
63+
"command": "react-toolkit.convertCjsToEsm",
64+
"group": "1_modification@2"
5865
}
5966
]
6067
}
6168
},
69+
"config": {
70+
"commitizen": {
71+
"path": "node_modules/cz-conventional-changelog"
72+
}
73+
},
6274
"scripts": {
75+
"cm": "git-cz",
6376
"vscode:prepublish": "yarn run compile",
6477
"compile": "tsc -p ./",
6578
"lint": "eslint src --ext ts",
6679
"watch": "tsc -watch -p ./",
6780
"pretest": "yarn run compile && yarn run lint",
6881
"test": "node ./out/test/runTest.js",
69-
"release": "npx standard-version"
82+
"release": "vsce publish",
83+
"version": "conventional-changelog -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md"
7084
},
7185
"devDependencies": {
7286
"@types/fs-extra": "^8.0.1",
@@ -76,13 +90,15 @@
7690
"@types/vscode": "^1.42.0",
7791
"@typescript-eslint/eslint-plugin": "^2.18.0",
7892
"@typescript-eslint/parser": "^2.18.0",
93+
"cz-conventional-changelog": "^3.1.0",
7994
"eslint": "^6.8.0",
8095
"glob": "^7.1.6",
8196
"mocha": "^7.0.1",
8297
"typescript": "^3.7.5",
8398
"vscode-test": "^1.3.0"
8499
},
85100
"dependencies": {
101+
"@wessberg/cjs-to-esm-transformer": "^0.0.18",
86102
"fs-extra": "^8.1.0",
87103
"react-js-to-ts": "^1.4.0"
88104
}

src/commands.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import * as vscode from 'vscode';
22
import * as fs from 'fs-extra';
33
import { run as convertToTypeScript } from 'react-js-to-ts';
4+
import { ModuleKind, transpileModule } from 'typescript';
5+
import { cjsToEsm } from '@wessberg/cjs-to-esm-transformer';
46

57
import { editorContext, showInputBox, camelCase, getNewFileName, isJsx } from './utils';
68

@@ -60,3 +62,29 @@ export const convertFileToTypescript = async (uri: vscode.Uri) => {
6062
return vscode.commands.executeCommand('editor.action.formatDocument');
6163
});
6264
};
65+
66+
export const convertCjsToEsm = async (uri: vscode.Uri) => {
67+
const { path } = uri;
68+
vscode.workspace
69+
.openTextDocument(uri)
70+
.then(async (document) => {
71+
const text = document.getText();
72+
const newFile = getNewFileName(path, isJsx(text) ? 'tsx' : 'ts');
73+
74+
const result = transpileModule(text, {
75+
transformers: cjsToEsm(),
76+
compilerOptions: {
77+
module: ModuleKind.ESNext,
78+
},
79+
});
80+
81+
await fs.rename(path, newFile);
82+
await fs.writeFile(newFile, result.outputText);
83+
84+
const file = await vscode.workspace.openTextDocument(newFile);
85+
vscode.window.showTextDocument(file);
86+
})
87+
.then(() => {
88+
return vscode.commands.executeCommand('editor.action.formatDocument');
89+
});
90+
};

src/extension.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import * as vscode from 'vscode';
2-
import { extractStyle, convertFileToTypescript } from './commands';
2+
import { extractStyle, convertFileToTypescript, convertCjsToEsm } from './commands';
33

44
export function activate(context: vscode.ExtensionContext) {
55
console.log('Congratulations, your extension "react-toolkit" is now active!');
66

77
const commandExtractStyle = vscode.commands.registerCommand('react-toolkit.extractStyle', extractStyle);
88
const commandReadFile = vscode.commands.registerCommand('react-toolkit.readFile', convertFileToTypescript);
9+
const commandCjsToEsm = vscode.commands.registerCommand('react-toolkit.convertCjsToEsm', convertCjsToEsm);
910

1011
context.subscriptions.push(commandExtractStyle);
1112
context.subscriptions.push(commandReadFile);
13+
context.subscriptions.push(commandCjsToEsm);
1214
}
1315

1416
export function deactivate() {}

0 commit comments

Comments
 (0)