Skip to content

Commit

Permalink
Merge dc91177 into 3347690
Browse files Browse the repository at this point in the history
  • Loading branch information
upupming committed Dec 28, 2020
2 parents 3347690 + dc91177 commit aad4061
Show file tree
Hide file tree
Showing 12 changed files with 1,407 additions and 88 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ jobs:
- name: Build artifact
run: |
mkdir artifact
yarn global add vsce
rm README.md && vsce package -o artifact
rm README.md && yarn vsce package --yarn -o artifact
- name: Upload artifact
uses: actions/upload-artifact@v1
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ yarn-error.log
package-lock.json
coverage
.coveralls.yml
.DS_Store
dist
8 changes: 3 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
"outFiles": ["${workspaceFolder}/dist/**/*.js"]
},
{
"name": "Extension Tests",
Expand All @@ -21,10 +20,9 @@
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/runner/index"
"--extensionTestsPath=${workspaceFolder}/dist/test/runner/index"
],
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
"outFiles": ["${workspaceFolder}/dist/test/**/*.js"]
}
]
}
3 changes: 3 additions & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ src/**
**/.eslintrc.json
**/*.map
**/*.ts
node_modules
out/
webpack.config.js
42 changes: 32 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": "https://github.com/PicGo/vs-picgo",
"issues": "https://github.com/PicGo/vs-picgo/issues",
"keywords": "%ext.keywords%",
"version": "2.0.4",
"version": "2.1.0",
"publisher": "Spades",
"engines": {
"vscode": "^1.43.0"
Expand All @@ -19,7 +19,7 @@
"onCommand:picgo.uploadImageFromExplorer",
"onCommand:picgo.uploadImageFromInputBox"
],
"main": "./out/src/extension.js",
"main": "./dist/extension",
"contributes": {
"commands": [
{
Expand Down Expand Up @@ -79,6 +79,21 @@
"markdownDescription": "%config.customOutputFormat.description%",
"default": "![${uploadedName}](${url})"
},
"picgo.picBed.uploader": {
"type": "string",
"enum": [
"smms",
"aliyun",
"github",
"imgur",
"qiniu",
"tcyun",
"upyun",
"weibo"
],
"default": "smms",
"markdownDescription": "%config.picBed.uploader%"
},
"picgo.picBed.current": {
"type": "string",
"enum": [
Expand All @@ -92,7 +107,7 @@
"weibo"
],
"default": "smms",
"markdownDescription": "%config.picBed.description%"
"markdownDescription": "%config.picBed.current%"
},
"picgo.picBed.smms.token": {
"type": "string",
Expand Down Expand Up @@ -271,10 +286,11 @@
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"test": "node ./out/test/run-test.js",
"vscode:prepublish": "webpack --mode production",
"compile": "webpack --mode development",
"watch": "webpack --mode development --watch",
"test-compile": "tsc -p ./",
"test": "tsc -p ./ && node ./out/test/run-test.js",
"lint": "eslint ./src --ext ts",
"codestyle:check": "prettier --check ./src/**/*.ts **/**.json",
"codestyle:fix": "prettier --write ./src/**/*.ts **/**.json",
Expand All @@ -283,7 +299,7 @@
},
"husky": {
"hooks": {
"pre-commit": "npm run codestyle:check && npm run lint",
"pre-commit": "yarn codestyle:check && yarn lint",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
},
Expand Down Expand Up @@ -312,6 +328,8 @@
"@types/vscode": "^1.43.0",
"@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.18.0",
"clean-webpack-plugin": "3.0.0",
"copy-webpack-plugin": "7.0.0",
"cz-conventional-changelog": "^3.1.0",
"cz-customizable": "6.2.0",
"decache": "^4.5.1",
Expand All @@ -324,13 +342,17 @@
"mocha": "^7.0.1",
"prettier": "^1.19.1",
"remap-istanbul": "^0.13.0",
"ts-loader": "8.0.12",
"typescript": "^3.7.5",
"vscode-test": "^1.3.0"
"vsce": "1.83.0",
"vscode-test": "^1.3.0",
"webpack": "5.11.0",
"webpack-cli": "4.3.0"
},
"dependencies": {
"lodash": "^4.17.19",
"lodash-id": "^0.14.0",
"picgo": "^1.4.7"
"picgo": "1.4.14"
},
"license": "MIT"
}
5 changes: 3 additions & 2 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
"command.upload.explorer.title": "Upload image from explorer",
"command.upload.inputBox.title": "Upload image from inputBox",
"config.title": "PicGo",
"config.configPath.description": "The path to your PicGo-Core configuration. PicGo will use `#picgo.picBed#` if this is not specified.",
"config.configPath.description": "The path to your PicGo-Core configuration. PicGo will use `#picgo.picBed#` if this is not specified. Please see [PicGo-Core Docs](https://picgo.github.io/PicGo-Core-Doc/zh/guide/config.html#%E9%BB%98%E8%AE%A4%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6) and [PicGo Docs](https://picgo.github.io/PicGo-Doc/zh/guide/config.html#%E5%9B%BE%E5%BA%8A%E5%8C%BA) for more detail about configuration file.",
"config.dataPath.description": "The path to your data file, including all uploaded images' info. PicGo will use `your_home_dir/vs-picgo-data.json` if this is not specified.",
"config.customUploadName.description": "Customize the name of the image to be uploaded, image will be renamed before uploading.\n- `${fileName}`: the name of the original image, without extension name.\n **Notice: If you selected some text before uploading, the selection will become the `fileName` of the image to be uploaded.**\n- `${extName}`: the extension name of the original image.\n- `${mdFileName}`: the name of the current editing markdown file.\n- `${date}`: YY-MM-DD formatted date.\n- `${dateTime}`: YY-MM-DD-hh-mm-ss formatted date.\n\nExamples:\n- `${fileName}-${date}${extName}` -> `picName-2016-07-25.jpg`\n- `${mdFileName}`-`${dateTime}${extName}` -> `markdownName-2017-04-12-22-28-10.jpg`",
"config.customOutputFormat.description": "Customize the output format of the uploaded image.\n- `${url}`: the url of the uploaded image.\n- `${uploadedName}`: the name of the uploaded image without extension name, see `#picgo.customUploadName#`, note that even if you used `${extName}` in `#picgo.customUploadName#`, there still will be no extension name in the output.\n\nExamples:\n- `![${uploadedName}](${url})` -> `![picName-2016-07-25](https://example.com/xxx.jpg)`\n- `<img src=\"${url}\" alt=\"${uploadedName}\">` -> `<img src=\"https://example.com/xxx.jpg\" alt=\"picName-2016-07-25\">`",
"config.picBed.description": "PicGo-Core configuration, please see [PicGo Docs](https://picgo.github.io/PicGo-Doc/zh/guide/config.html#%E5%9B%BE%E5%BA%8A%E5%8C%BA).",
"config.picBed.uploader": "Please see [`picBed.uploader`](https://picgo.github.io/PicGo-Core-Doc/zh/guide/config.html#picbed-uploader).",
"config.picBed.current": "Please see [`picBed.current`](https://picgo.github.io/PicGo-Core-Doc/zh/guide/config.html#picbed-current).",
"config.picBed.smms.description": "SM.MS picBed configuration.",
"config.picBed.weibo.description": "Weibo picBed configuration.",
"config.picBed.qiniu.description": "Qiniu picBed configuration.",
Expand Down
6 changes: 3 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import * as path from 'path';
import * as vscode from 'vscode';
import VSPicgo from './vs-picgo';

function uploadImageFromClipboard(vspicgo: VSPicgo): Promise<string | void | Error> {
function uploadImageFromClipboard(vspicgo: VSPicgo) {
return vspicgo.upload();
}

async function uploadImageFromExplorer(vspicgo: VSPicgo): Promise<string | void | Error> {
async function uploadImageFromExplorer(vspicgo: VSPicgo) {
const result = await vscode.window.showOpenDialog({
filters: {
Images: ['png', 'jpg', 'jpeg', 'webp', 'gif', 'bmp', 'tiff', 'ico', 'svg'],
Expand All @@ -22,7 +22,7 @@ async function uploadImageFromExplorer(vspicgo: VSPicgo): Promise<string | void
}
}

async function uploadImageFromInputBox(vspicgo: VSPicgo): Promise<string | void | Error> {
async function uploadImageFromInputBox(vspicgo: VSPicgo) {
let result = await vscode.window.showInputBox({
placeHolder: 'Please input an image location path',
});
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import { IUploadName, IOutputUrl } from '../vs-picgo';
import { window } from 'vscode';
import { ImgInfo } from 'picgo/dist/src/utils/interfaces';
import { IImgInfo } from 'picgo/dist/src/utils/interfaces';

export function formatParam(file: string, mdFileName: string): IUploadName {
const dt = new Date();
Expand Down Expand Up @@ -63,7 +63,7 @@ export function showInfo(messgae: string) {
* extname will be removed for the sake of simplicity when used as alt.
* @param imgInfo
*/
export function getUploadedName(imgInfo: ImgInfo): string {
export function getUploadedName(imgInfo: IImgInfo): string {
let fullName;
if (!imgInfo.fileName) {
fullName = '';
Expand Down
41 changes: 25 additions & 16 deletions src/vs-picgo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as os from 'os';
import * as vscode from 'vscode';

import PicGo from 'picgo/dist/src/core/PicGo';
import { ImgInfo, Plugin } from 'picgo/dist/src/utils/interfaces';
import { IImgInfo, IPlugin, IConfig as IPicGoConfig } from 'picgo/dist/src/utils/interfaces';

import { promisify } from 'util';

Expand Down Expand Up @@ -58,18 +58,22 @@ export default class VSPicgo extends EventEmitter {

configPicgo() {
const picgoConfigPath = vscode.workspace.getConfiguration('picgo').get<string>('configPath');
let config: Partial<IPicGoConfig>;
if (picgoConfigPath) {
VSPicgo.picgo.setConfig(
JSON.parse(
fs.readFileSync(picgoConfigPath, {
encoding: 'utf-8',
}),
),
config = JSON.parse(
fs.readFileSync(picgoConfigPath, {
encoding: 'utf-8',
}),
);
} else {
const picBed = vscode.workspace.getConfiguration('picgo.picBed');
VSPicgo.picgo.setConfig({ picBed });
const picBed = (vscode.workspace.getConfiguration('picgo.picBed') as any) as IPicGoConfig['picBed'];
config = { picBed };
}

// `PICGO_ENV` is used only by Electron version, should be disabled here,
// see https://github.com/PicGo/vs-picgo/issues/75 for more detail
(config as any)['PICGO_ENV'] = 'CLI';
VSPicgo.picgo.setConfig(config);
}

addGenerateOutputListener() {
Expand All @@ -78,7 +82,7 @@ export default class VSPicgo extends EventEmitter {
const outputFormatTemplate =
vscode.workspace.getConfiguration('picgo').get<string>('customOutputFormat') || '![${uploadedName}](${url})';
try {
urlText = ctx.output.reduce((acc: string, imgInfo: ImgInfo): string => {
urlText = ctx.output.reduce((acc: string, imgInfo: IImgInfo): string => {
return `${acc}${formatString(outputFormatTemplate, {
uploadedName: getUploadedName(imgInfo),
url: imgInfo.imgUrl,
Expand Down Expand Up @@ -106,14 +110,14 @@ export default class VSPicgo extends EventEmitter {
}

registerRenamePlugin() {
let beforeUploadPlugin: Plugin = {
let beforeUploadPlugin: IPlugin = {
handle: (ctx: PicGo) => {
const uploadNameTemplate =
vscode.workspace.getConfiguration('picgo').get<string>('customUploadName') || '${fileName}';
if (ctx.output.length === 1) {
ctx.output[0].fileName = this.changeFilename(ctx.output[0].fileName || '', uploadNameTemplate, undefined);
} else {
ctx.output.forEach((imgInfo: ImgInfo, index: number) => {
ctx.output.forEach((imgInfo: IImgInfo, index: number) => {
imgInfo.fileName = this.changeFilename(imgInfo.fileName || '', uploadNameTemplate, index);
});
}
Expand Down Expand Up @@ -161,7 +165,7 @@ export default class VSPicgo extends EventEmitter {
}
}

async upload(input?: string[]): Promise<string | void | Error> {
async upload(input?: string[]) {
// This is necessary, because user may have changed settings
this.configPicgo();

Expand All @@ -173,13 +177,17 @@ export default class VSPicgo extends EventEmitter {
cancellable: false,
},
progress => {
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
VSPicgo.picgo.on('uploadProgress', (p: number) => {
progress.report({ increment: p });
if (p === 100) {
resolve();
}
});
VSPicgo.picgo.on('failed', (error: Error) => {
showError(error.message || 'Unknown error');
reject();
});
VSPicgo.picgo.on('notification', (notice: INotice) => {
showError(`${notice.title}! ${notice.body || ''}${notice.text || ''}`);
reject();
Expand All @@ -188,10 +196,11 @@ export default class VSPicgo extends EventEmitter {
},
);

return VSPicgo.picgo.upload(input);
// Error has been handled in on 'failed'
return VSPicgo.picgo.upload(input).catch(() => {});
}

async updateData(picInfos: Array<ImgInfo>) {
async updateData(picInfos: Array<IImgInfo>) {
const dataPath = this.dataPath;
if (!fs.existsSync(dataPath)) {
await this.initDataFile(dataPath);
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"strict": true,
/* enable all strict type-checking options */
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */
"noUnusedLocals": true /* Report errors on unused locals. */,
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"skipLibCheck": true
},
"include": ["./src/**/*", "./test/**/*"],
"exclude": ["node_modules", ".vscode-test"]
Expand Down
50 changes: 50 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//@ts-check
// https://code.visualstudio.com/api/working-with-extensions/bundling-extension#configure-webpack

'use strict';

const path = require('path');
const CopyPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

/**@type {import('webpack').Configuration}*/
const config = {
target: 'node', // vscode extensions run in a Node.js-context 📖 -> https://webpack.js.org/configuration/node/

entry: './src/extension.ts', // the entry point of this extension, 📖 -> https://webpack.js.org/configuration/entry-context/
output: {
// the bundle is stored in the 'dist' folder (check package.json), 📖 -> https://webpack.js.org/configuration/output/
path: path.resolve(__dirname, 'dist'),
filename: 'extension.js',
libraryTarget: 'commonjs2',
devtoolModuleFilenameTemplate: '../[resource-path]',
},
devtool: 'source-map',
externals: {
vscode: 'commonjs vscode', // the vscode-module is created on-the-fly and must be excluded. Add other modules that cannot be webpack'ed, 📖 -> https://webpack.js.org/configuration/externals/
},
resolve: {
// support reading TypeScript and JavaScript files, 📖 -> https://github.com/TypeStrong/ts-loader
extensions: ['.ts', '.js', '.json'],
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
use: [
{
loader: 'ts-loader',
},
],
},
],
},
plugins: [
new CopyPlugin({
patterns: [{ from: 'node_modules/picgo/dist/src/utils/clipboard', to: 'clipboard' }],
}),
new CleanWebpackPlugin(),
],
};
module.exports = config;
Loading

0 comments on commit aad4061

Please sign in to comment.