Skip to content

Commit

Permalink
feat(Config): Prompt list of files to config extension (#22)
Browse files Browse the repository at this point in the history
* feat(Config): Prompt list of files to config extension

* feat(Command): Add command to update config
  • Loading branch information
MarioCadenas authored Oct 5, 2020
1 parent 41768f1 commit 05dbe2c
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 49 deletions.
77 changes: 31 additions & 46 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,46 @@
const vscode = require("vscode");
const { ConfigParser } = require("./src/parser");
const { javascriptProvider } = require("./src/providers/javascript");
const { getConfig } = require("./src/config");
const vscode = require('vscode');
const registerCommands = require('./src/commands');
const registerProviders = require('./src/providers');
const { ConfigParser } = require('./src/parser');
const { getConfig, configureFile } = require('./src/config');
const { ACTIONS } = require('./src/constants');

/**
* @param {vscode.ExtensionContext} context
*/
function activate(context) {
async function activate(context) {
registerCommands();

const { file } = getConfig();
let fileName;

if (!file) {
return vscode.window.showInformationMessage(
`Welcome to alias-resolver extension! Define which file you want to use to resolve alias in your user/workspace settings!`
const prompt = await vscode.window.showInformationMessage(
`Welcome to alias-resolver extension! Do you want to configure the extension now?`,
ACTIONS.CONFIGURE,
ACTIONS.CLOSE
);
}

vscode.workspace.findFiles(file, "**/node_modules/**").then(([{ path }]) => {
vscode.workspace.openTextDocument(path).then((document) => {
vscode.window.showInformationMessage(
`Using ${file} to resolve alias paths`
);
const workspace = vscode.workspace.getWorkspaceFolder(document.uri);
const ctx = {
__dirname: workspace.uri.path,
__process: {
cwd() {
return vscode.workspace.rootPath;
},
},
path: require("path"),
};
const text = document.getText();

ConfigParser.createMappingsFromConfig(text, ctx);
if (!prompt || prompt === ACTIONS.CLOSE) {
return;
}

const {
provideCompletionItems,
provideDefinition,
} = javascriptProvider.providers;

const completionItemDisposable = vscode.languages.registerCompletionItemProvider(
javascriptProvider.selector,
{ provideCompletionItems },
...(javascriptProvider.triggerCharacters || [])
);

const definitionDisposable = vscode.languages.registerDefinitionProvider(
javascriptProvider.selector,
{ provideDefinition }
);
fileName = await configureFile();
}

context.subscriptions.push(
completionItemDisposable,
definitionDisposable
);
vscode.workspace
.findFiles(fileName || file, '**/node_modules/**')
.then(([{ path }]) => {
vscode.workspace.openTextDocument(path).then((document) => {
vscode.window.showInformationMessage(
`Using ${fileName || file} to resolve alias paths`
);
const text = document.getText();

ConfigParser.createMappingsFromConfig(text);
registerProviders(context);
});
});
});
}
exports.activate = activate;

Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,20 @@
"title": "alias-resolver",
"type": "object",
"properties": {
"file": {
"alias-resolver.file": {
"type": "string",
"default": "",
"description": "File with alias mappings such as webpack.js or rollup.config.js"
}
}
}
},
"commands": [
{
"command": "alias-resolver.updateConfigFile",
"title": "Update configuration file",
"category": "Alias resolver"
}
]
},
"scripts": {
"lint": "eslint .",
Expand Down
11 changes: 11 additions & 0 deletions src/commands/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const vscode = require('vscode');
const { configureFile } = require('../config');

function registerCommands() {
vscode.commands.registerCommand(
'alias-resolver.updateConfigFile',
configureFile
);
}

module.exports = { registerCommands };
1 change: 1 addition & 0 deletions src/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./commands').registerCommands;
30 changes: 30 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const vscode = require('vscode');
const path = require('path');

function getConfig() {
const { file, path } = vscode.workspace.getConfiguration('alias-resolver');
Expand All @@ -9,6 +10,35 @@ function getConfig() {
};
}

function updateConfig(file) {
const config = vscode.workspace.getConfiguration('alias-resolver');

return config.update('file', file, vscode.ConfigurationTarget.Workspace);
}

async function configureFile() {
const result = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
});

if (!result) {
return;
}

const [selectedFile] = result;
const { path: selectedFilePath } = selectedFile;

const fileName = path.basename(selectedFilePath);

await updateConfig(fileName);

return fileName;
}

module.exports = {
getConfig,
updateConfig,
configureFile,
};
5 changes: 5 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const ACTIONS = {
CONFIGURE: 'Configure',
CLOSE: 'Close',
};
const DEFAULT_FILE = 'index';
const DEFAULT_STYLES_FILE = 'style';

module.exports = {
ACTIONS,
DEFAULT_FILE,
DEFAULT_STYLES_FILE,
};
2 changes: 1 addition & 1 deletion src/parser/config-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const walk = require('acorn-walk');
const { alias } = require('../alias/alias-map');

class ConfigParser {
static createMappingsFromConfig(rawText, ctx) {
static createMappingsFromConfig(rawText) {
const ast = Parser.parse(rawText);

walk.simple(ast, {
Expand Down
1 change: 1 addition & 0 deletions src/providers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./register-providers').registerProviders;
24 changes: 24 additions & 0 deletions src/providers/register-providers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const vscode = require('vscode');
const { javascriptProvider } = require('./javascript');

function registerProviders(context) {
const {
provideCompletionItems,
provideDefinition,
} = javascriptProvider.providers;

const completionItemDisposable = vscode.languages.registerCompletionItemProvider(
javascriptProvider.selector,
{ provideCompletionItems },
...(javascriptProvider.triggerCharacters || [])
);

const definitionDisposable = vscode.languages.registerDefinitionProvider(
javascriptProvider.selector,
{ provideDefinition }
);

context.subscriptions.push(completionItemDisposable, definitionDisposable);
}

module.exports = { registerProviders };

0 comments on commit 05dbe2c

Please sign in to comment.