Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
matiduda committed Nov 12, 2023
1 parent 401dda9 commit 2f5a163
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 33 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
dist/
dist/
.scope/
coverage/
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ npm i scope-tags -D
- [x] Add support for multiple tsconfig.json's
- [ ] Add tests for this ?
- [x] `.scope` metadata initialization
- [ ] `tags.json`
- [ ] `database.json`
- [x] `tags.json`
- [x] `database.json`
- [ ] Basic command line interface and tag management:
- [ ] Reading `tags.json`
- [ ] Adding, deleting, updating tags
- [x] Basic file to module mapping
- [ ] Tags should have (possibly nested) categories
- [ ] Add [np package](https://www.npmjs.com/package/np) to handle publishing to npm

### Assertions to add

- [ ] On loading `tags.json` assert that all parents exist in database, if not then these modules won't be displayed

### Nice to have

- [ ] If eslint available, compare changed files before and after linting. Then, ommit files which only have these changes from scope report.
Expand Down
6 changes: 5 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transformIgnorePatterns: ['<rootDir>/node_modules/'],
roots: ["<rootDir>/test/"]
roots: ["<rootDir>/test/", "<rootDir>/src/"],
collectCoverage: true,
collectCoverageFrom: ["**/src/**", "!**/node_modules/**"],
coverageDirectory: './coverage',
coverageReporters: ['json', 'lcovonly', 'text', 'clover']
};
13 changes: 10 additions & 3 deletions src/Console/Menu.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { TagManager } from "./TagManager";
import { FileTagsDatabase } from "../Scope/FileTagsDatabase";
import { ConfigFile } from "../Scope/ConfigFile";
import { TagsDefinitionFile } from "../Scope/TagsDefinitionFile";
import { Module, TagsDefinitionFile } from "../Scope/TagsDefinitionFile";
import { ModuleManager } from "./ModuleManager";

const { Select } = require('enquirer')

export class Menu {

private _config: ConfigFile;
Expand All @@ -20,7 +21,7 @@ export class Menu {
public async start() {
const prompt = new Select({
name: 'Menu',
message: "Scope Tags ",
message: "Scope Tags",
choices: [
{ name: 'Start', value: this.start },
{ name: 'Manage tags', value: this._manageTags },
Expand All @@ -41,11 +42,17 @@ export class Menu {
}
}

private async _manageTags() {
public async _manageTags() {
const tagManager = new TagManager(this._tags, this);
await tagManager.start();
}

public async manageTagsFromModule(module: Module) {
const tagManager = new TagManager(this._tags, this);
await tagManager.manageTagsFromModule(module);
}


private async _manageModules() {
const tagManager = new ModuleManager(this._tags, this);
await tagManager.start();
Expand Down
64 changes: 47 additions & 17 deletions src/Console/ModuleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ export class ModuleManager {

const prompt = new Select({
name: 'Module manager',
message: this._getHeaderFromModule(fromModule),
message: this._getModulePathAsHeader(fromModule),
choices: [
...choices,
{ name: 'New module', value: this._addModule },
{ name: 'Add tag', value: this._addTagToModule },
fromModule ? {
name: "Back to " + fromModule.name, value: this._goBack
} : {
name: "Main menu", value: this._exit
}
{ role: 'separator' },
{ name: 'Add new module here', value: this._addModule },
...fromModule ? [
{ name: 'Manage tags of: ' + fromModule.name, value: this._manageTagsFromModule },
{ name: 'Delete this module', value: this._deleteModule },
{ name: "Back to: " + (fromModule.parent || "root"), value: this._goBack },
] : [
{ name: "Back to menu", value: this._exit }
],
],
result(value: any) {
const mapped = this.map(value);
Expand Down Expand Up @@ -75,11 +77,11 @@ export class ModuleManager {
},
});

const isExclusivePrompt = await new Confirm({
const isExclusivePrompt = new Confirm({
name: "exclusive",
message: 'Should the module be exclusive?\n(Meaning: only one tag per file can be added from this module)',
question: '2 Should the module be exclusive?\n(Meaning: only one tag per file can be added from this module)',
}).run();
});


try {
Expand All @@ -105,6 +107,30 @@ export class ModuleManager {
await this.start.call(this, fromModule);
}

private async _deleteModule(fromModule: Module) {
let returnModule: string = fromModule.name;

try {
this._tags.deleteModule(fromModule);
this._modulesWereModified = true;
returnModule = fromModule.parent || "";
console.log(`Deleted module ${fromModule.name}`);
} catch (e) {
console.log((e as Error).message);
}
await this.start(this._tags.getModuleByName(returnModule));
}

private async _manageTagsFromModule(module?: Module) {
if (!module) {
console.log("Cannot manage tags of undefined module");
return;
}

await this._menu.manageTagsFromModule(module);
await this.start(module);
}

private async _goBack(fromModule?: Module) {
if (!fromModule) {
throw new Error("Can't go back to undefind module");
Expand All @@ -126,14 +152,18 @@ export class ModuleManager {
return modules.map(module => ({ name: module.name, value: module }));
}

private _getHeaderFromModule(module: Module): string {
if (!module.parent) {
return `${module.name}`;
private _getModulePathAsHeader(module?: Module): string {
let header = "Root >";

if (!module) {
return header;
}
let header = "";
const moduleParents = this._tags.getModuleParents(module);
moduleParents.forEach(module => header += `${module.name} > `);
header += module.name;

const parentList = this._tags.getModuleParentNames(module);

parentList.push(module.name);
parentList.forEach(parent => header += ` ${parent} >`);

return header;
}
}
11 changes: 8 additions & 3 deletions src/Console/TagManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TagsDefinitionFile } from "../Scope/TagsDefinitionFile";
import { Module, TagsDefinitionFile } from "../Scope/TagsDefinitionFile";
import { Menu } from "./Menu";

const { Select, Toggle } = require('enquirer')
Expand All @@ -20,8 +20,8 @@ export class TagManager {
name: 'Tag manager',
message: 'Manage tags',
choices: [
{ name: 'By name', value: this._manageTagsByName },
{ name: 'By module', value: this._manageTagsByModule },
// { name: 'By name', value: this._manageTagsByName },
// { name: 'By module', value: this._manageTagsByModule },
{ name: "Main menu", value: this._exit }
],
result(value: any) {
Expand All @@ -34,6 +34,11 @@ export class TagManager {
await answer.call(this);
}

public manageTagsFromModule(module: Module) {
console.log("Tags of module: " + module.name);

}

private async _exit() {
if (this._tagsWereModified) {
this._tags.save();
Expand Down
35 changes: 30 additions & 5 deletions src/Scope/TagsDefinitionFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ export class TagsDefinitionFile implements IJSONFileDatabase<TagsDefinitionFile>
this._tagsDatabaseData.modules.push(newModule);
}

public deleteModule(moduleToDelete: Module) {
if (!moduleToDelete) {
throw new Error("Can't remove undefined module");
}
if (moduleToDelete.tags.length) {
const tags = moduleToDelete.tags.map(tag => tag.name);
throw new Error(`
Can't remove module ${moduleToDelete.name} which has tags: ${tags.join(", ")}`
);
}
if (moduleToDelete.children.length) {
const children = moduleToDelete.children.join(", ");
throw new Error(`
Can't remove module ${moduleToDelete.name} with child modules: ${children}`
);
}
const moduleToDeleteIndex = this._tagsDatabaseData.modules.indexOf(moduleToDelete);
if (moduleToDeleteIndex === -1) {
throw new Error(`Module ${moduleToDelete.name} not found in database`);
}

const parentModule = this.getModuleByName(moduleToDelete.parent);
if (parentModule) {
parentModule.children.splice(parentModule.children.indexOf(moduleToDelete.name), 1);
}
this._tagsDatabaseData.modules.splice(moduleToDeleteIndex, 1);
}

public getTags(): Set<Tag> {
return this._allTags;
Expand Down Expand Up @@ -111,10 +138,10 @@ export class TagsDefinitionFile implements IJSONFileDatabase<TagsDefinitionFile>
return this._tagsDatabaseData.modules.filter(module => children.includes(module.name));
}

public getModuleParents(module: Module) {
public getModuleParentNames(module: Module): Array<string> {
const parents = [];
while (!module.parent) {
parents.push(module);
while (module.parent) {
parents.push(module.parent);
module = this.getModuleByName(module.parent) || {} as Module;
}
return parents;
Expand All @@ -127,7 +154,6 @@ export class TagsDefinitionFile implements IJSONFileDatabase<TagsDefinitionFile>
description: "Default tag added on initialization",
module: TagsDefinitionFile.getDefaultModule().name,
};

return defaultTag;
}

Expand All @@ -140,7 +166,6 @@ export class TagsDefinitionFile implements IJSONFileDatabase<TagsDefinitionFile>
parent: null,
children: [],
}

return defaultModule;
}
}
1 change: 0 additions & 1 deletion src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { YesNoMenu } from "./Console/YesNoMenu";

// Will be needed to get output from script
const [, , ...args] = process.argv;
console.log("scope tags v0.0.2 " + args);

// Find git repository
const root: string = getGitProjectRoot();
Expand Down

0 comments on commit 2f5a163

Please sign in to comment.