-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alexganggao
committed
Jun 26, 2020
1 parent
2decd0e
commit 281cde6
Showing
18 changed files
with
1,192 additions
and
1 deletion.
There are no files selected for viewing
Submodule vscode-plugin-json-to-ts
deleted from
06db72
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
out | ||
*.vsix |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// A launch configuration that compiles the extension and then opens it inside a new window | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Run Extension", | ||
"type": "extensionHost", | ||
"request": "launch", | ||
"runtimeExecutable": "${execPath}", | ||
"args": ["--extensionDevelopmentPath=${workspaceRoot}"], | ||
"outFiles": ["${workspaceFolder}/out/**/*.js"], | ||
"preLaunchTask": "npm: watch" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.insertSpaces": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "npm", | ||
"script": "watch", | ||
"problemMatcher": "$tsc-watch", | ||
"isBackground": true, | ||
"presentation": { | ||
"reveal": "never" | ||
}, | ||
"group": { | ||
"kind": "build", | ||
"isDefault": true | ||
} | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
# JSON转换成typescript的interface | ||
|
||
## 特色 | ||
|
||
1. 从剪切板json数据转换成interface (windows: `ctrl+alt+C ` , Mac : `^+⌥+C`) | ||
|
||
![](https://user-gold-cdn.xitu.io/2020/6/10/1729d10869716f54?w=360&h=240&f=gif&s=153424) | ||
|
||
2. 选择json数据转换成interface (windows: `ctrl+alt+S ` , Mac : `^+⌥+S`) | ||
|
||
![](https://user-gold-cdn.xitu.io/2020/6/10/1729d10d702f391a?w=360&h=240&f=gif&s=147274) | ||
|
||
|
||
3. 将json文件转换成interface (windows: `ctrl+alt+F ` , Mac : `^+⌥+F`) | ||
|
||
![](https://user-gold-cdn.xitu.io/2020/6/10/1729d11313973504?w=360&h=240&f=gif&s=163609) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.activate = void 0; | ||
var path = __importStar(require("path")); | ||
var os = __importStar(require("os")); | ||
var fs = __importStar(require("fs")); | ||
var vscode_1 = require("vscode"); | ||
var json_to_ts_1 = __importDefault(require("json-to-ts")); | ||
var lib_1 = require("./lib"); | ||
function activate(context) { | ||
context.subscriptions.push(vscode_1.commands.registerCommand("jsonToTs.fromSelection", transformFromSelection)); | ||
context.subscriptions.push(vscode_1.commands.registerCommand("jsonToTs.fromClipboard", transformFromClipboard)); | ||
context.subscriptions.push(vscode_1.commands.registerCommand("jsonToTs.fromJSONFile", transformFromJSONFile)); | ||
} | ||
exports.activate = activate; | ||
function transformFromSelection() { | ||
var tmpFilePath = path.join(os.tmpdir(), "json-to-ts.ts"); | ||
var tmpFileUri = vscode_1.Uri.file(tmpFilePath); | ||
lib_1.getSelectedText() | ||
.then(lib_1.validateLength) | ||
.then(lib_1.parseJson) | ||
.then(function (json) { | ||
return json_to_ts_1.default(json).reduce(function (a, b) { return a + "\n\n" + b; }); | ||
}) | ||
.then(function (interfaces) { | ||
fs.writeFileSync(tmpFilePath, interfaces); | ||
}) | ||
.then(function () { | ||
vscode_1.commands.executeCommand("vscode.open", tmpFileUri, lib_1.getViewColumn()); | ||
}) | ||
.catch(lib_1.handleError); | ||
} | ||
function transformFromJSONFile() { | ||
var _a; | ||
var activeFile = (_a = vscode_1.window.activeTextEditor) === null || _a === void 0 ? void 0 : _a.document.fileName; | ||
var filePath = activeFile === null || activeFile === void 0 ? void 0 : activeFile.substring(0, activeFile === null || activeFile === void 0 ? void 0 : activeFile.lastIndexOf("/")); | ||
var tmpFilePath = path.join(filePath ? filePath : os.tmpdir(), "json-to-ts.ts"); | ||
var tmpFileUri = vscode_1.Uri.file(tmpFilePath); | ||
lib_1.getSelectedFile() | ||
.then(lib_1.parseJson) | ||
.then(function (json) { | ||
return json_to_ts_1.default(json).reduce(function (a, b) { return a + "\n\n" + b; }); | ||
}) | ||
.then(function (interfaces) { | ||
fs.writeFileSync(tmpFilePath, interfaces); | ||
}) | ||
.then(function () { | ||
vscode_1.commands.executeCommand("vscode.open", tmpFileUri, lib_1.getViewColumn()); | ||
}) | ||
.catch(lib_1.handleError); | ||
} | ||
function transformFromClipboard() { | ||
lib_1.getClipboardText() | ||
.then(lib_1.validateLength) | ||
.then(lib_1.parseJson) | ||
.then(function (json) { | ||
return json_to_ts_1.default(json).reduce(function (a, b) { return a + "\n\n" + b; }); | ||
}) | ||
.then(function (interfaces) { | ||
lib_1.pasteToMarker(interfaces); | ||
}) | ||
.catch(lib_1.handleError); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateLength = exports.getSelectedFile = exports.getSelectedText = exports.pasteToMarker = exports.getViewColumn = exports.parseJson = exports.handleError = exports.getClipboardText = void 0; | ||
var vscode_1 = require("vscode"); | ||
var copyPaste = __importStar(require("copy-paste")); | ||
var lodash_1 = __importDefault(require("lodash")); | ||
var fs_1 = __importDefault(require("fs")); | ||
function getClipboardText() { | ||
try { | ||
return Promise.resolve(copyPaste.paste()); | ||
} | ||
catch (error) { | ||
return Promise.reject(error); | ||
} | ||
} | ||
exports.getClipboardText = getClipboardText; | ||
function handleError(error) { | ||
vscode_1.window.showErrorMessage(error.message); | ||
} | ||
exports.handleError = handleError; | ||
function parseJson(json) { | ||
var tryEval = function (str) { return eval("const a = " + str + "; a"); }; | ||
try { | ||
return Promise.resolve(JSON.parse(json)); | ||
} | ||
catch (ignored) { } | ||
try { | ||
return Promise.resolve(tryEval(json)); | ||
} | ||
catch (error) { | ||
return Promise.reject(new Error("JSON 格式 无效")); | ||
} | ||
} | ||
exports.parseJson = parseJson; | ||
function getViewColumn() { | ||
var activeEditor = vscode_1.window.activeTextEditor; | ||
if (!activeEditor) { | ||
return vscode_1.ViewColumn.One; | ||
} | ||
switch (activeEditor.viewColumn) { | ||
case vscode_1.ViewColumn.One: | ||
return vscode_1.ViewColumn.Two; | ||
case vscode_1.ViewColumn.Two: | ||
return vscode_1.ViewColumn.Three; | ||
} | ||
return activeEditor.viewColumn; | ||
} | ||
exports.getViewColumn = getViewColumn; | ||
function pasteToMarker(content) { | ||
var activeTextEditor = vscode_1.window.activeTextEditor; | ||
return activeTextEditor === null || activeTextEditor === void 0 ? void 0 : activeTextEditor.edit(function (editBuilder) { | ||
editBuilder.replace(activeTextEditor.selection, content); | ||
}); | ||
} | ||
exports.pasteToMarker = pasteToMarker; | ||
function getSelectedText() { | ||
var _a = vscode_1.window.activeTextEditor, selection = _a.selection, document = _a.document; | ||
return Promise.resolve(document.getText(selection).trim()); | ||
} | ||
exports.getSelectedText = getSelectedText; | ||
function getSelectedFile() { | ||
var document = vscode_1.window.activeTextEditor.document; | ||
if (lodash_1.default.endsWith(document.fileName, "json")) { | ||
return Promise.resolve(fs_1.default.readFileSync(document.fileName, 'utf8').toString()); | ||
} | ||
return Promise.resolve(""); | ||
} | ||
exports.getSelectedFile = getSelectedFile; | ||
exports.validateLength = function (text) { | ||
if (text.length === 0) { | ||
return Promise.reject(new Error("Nothing selected")); | ||
} | ||
else { | ||
return Promise.resolve(text); | ||
} | ||
}; |
Oops, something went wrong.