Skip to content

Commit

Permalink
Merge pull request #71 from iamarnas/master
Browse files Browse the repository at this point in the history
Bugfix
  • Loading branch information
iamarnas authored Nov 3, 2021
2 parents 0514b98 + 77a37f2 commit 23ef6f1
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 33 deletions.
13 changes: 0 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,6 @@
{
"avoidEscape": true
}
],
"sort-imports": [
2,
{
"ignoreCase": false,
"ignoreMemberSort": false,
"memberSyntaxSortOrder": [
"none",
"all",
"multiple",
"single"
]
}
]
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.5.1

- Bugfix.

## 3.5.0

- Added support for safe `JSON`. The generator can read and parse `json` and `jsonc` from any method.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "json-to-dart",
"displayName": "Json to Dart Model",
"description": "Extension convert Json to Dart Model class",
"version": "3.5.0",
"version": "3.5.1",
"publisher": "hirantha",
"icon": "icon.png",
"engines": {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ export const generateClass = async (settings: Settings) => {
if (!fm.existsSync(path)) {
await fm.createDirectory(path);
}

await createClass(settings);
};
10 changes: 5 additions & 5 deletions src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as copyPaste from 'copy-paste';
import * as path from 'path';

import { ViewColumn, window } from 'vscode';
import { ClassDefinition } from './syntax';
Expand Down Expand Up @@ -94,20 +95,19 @@ export async function createClass(settings: Settings) {
var modelGenerator = new ModelGenerator(settings);
var classes: ClassDefinition[] = await modelGenerator.generateDartClasses(settings.json);

for (var i = 0; i < classes.length; ++i) {
const classDef = classes[i];
for await (var classDef of classes) {
const enhancement = settings.model.enhancement;
const fileName = `${classDef.path}${enhancement}.dart`;
const path = `${settings.targetDirectory}/${fileName}`;
const file = path.join(settings.targetDirectory, fileName);

if (fm.existsSync(path)) {
if (fm.existsSync(file)) {
window.showInformationMessage(`${fileName} already exists`);
} else {
const data = settings.input.generate ?
classDef.toCodeGenString(settings.input) :
classDef.toString(settings.input);

await fm.writeFile(path, data);
await fm.writeFile(file, data);
}
}
}
33 changes: 22 additions & 11 deletions src/utils/file-manager.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import * as fs from 'fs';
import * as mkdirp from 'mkdirp';

import { getWorkspaceRoot } from './get-workspace-root';
import { fsPath, getWorkspaceRoot } from './workspace';
import { window } from 'vscode';

type WriteFileOptions = { showError?: string, showInfo?: string };

export class FileManager {

/** The path for reading directory. */
get workspaceRoot() {
return getWorkspaceRoot();
}

/** The path for reading files. */
get fsPath() {
return fsPath();
}

/**
* Creates a path securely by automatically adding the root path of the workspace if it is missing.
* @param {string} path A path to a file or directory.
* @returns A string.
*/
safePath(path: string): string {
safeRootPath(path: string): string {
const root = `${this.workspaceRoot}`;
return path.startsWith(root) ? path : `${root}${path}`;
}

safeFsPath(path: string): string {
const fsPath = `${this.fsPath}`;
return path.startsWith(fsPath) ? path : `${fsPath}${path}`;
}

existsSync(path: string): boolean {
return fs.existsSync(this.safePath(path));
return fs.existsSync(this.safeFsPath(path));
}

/**
Expand All @@ -32,7 +43,7 @@ export class FileManager {
*/
readDirectory(dir: string): string[] {
try {
return fs.readdirSync(this.safePath(dir), 'utf-8');
return fs.readdirSync(this.safeRootPath(dir), 'utf-8');
} catch (_) {
return [];
}
Expand All @@ -46,7 +57,7 @@ export class FileManager {
if (!this.existsSync(dir)) { return; }

const entries = this.readDirectory(dir);
const directory = this.safePath(dir);
const directory = this.safeRootPath(dir);

if (entries.length > 0) {
for (const file of entries) {
Expand All @@ -59,36 +70,36 @@ export class FileManager {
}

createDirectory(dir: string): Promise<void> {
const path = this.safePath(dir);
const path = this.safeRootPath(dir);

return new Promise(async (resolve, reject) => {
await mkdirp(path)
.then((_) => resolve())
.catch((err) => reject(err));
.catch((err) => reject(new Error(`Couldn't create directory due to error: ${err}`)));
});
}

removeFile(path: string): void {
const dir = this.safePath(path);
const dir = this.safeFsPath(path);
fs.unlinkSync(dir);
}

readFile(path: string): string {
const dir = this.safePath(path);
const dir = this.safeFsPath(path);
const data = fs.readFileSync(dir, 'utf-8');
return data;
}

writeFile(path: string, data: string, options?: WriteFileOptions) {
const dir = this.safePath(path);
const dir = this.safeFsPath(path);

return new Promise<void>((resolve, reject) => {
fs.writeFile(dir, data, 'utf8', (err) => {
if (err) {
if (options?.showError) {
window.showErrorMessage(options.showError);
} else {
reject(err);
reject(new Error(`Couldn't create file due to error: ${err}`));
}
} else {
if (options?.showInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './get-workspace-root';
export * from './workspace';
export * from './file-manager';
export * from './helper';
14 changes: 13 additions & 1 deletion src/utils/get-workspace-root.ts → src/utils/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import * as vscode from 'vscode';

/** The path for reading directory. */
export function getWorkspaceRoot(): string | undefined {
const workspaceRoot = (vscode.workspace.workspaceFolders && (vscode.workspace.workspaceFolders.length > 0))
? vscode.workspace.workspaceFolders[0].uri.fsPath : undefined;
? vscode.workspace.workspaceFolders[0].uri.path : undefined;
if (!workspaceRoot) {
vscode.window.showErrorMessage("Couldn't find the workspace root directory");
return;
}
return workspaceRoot;
}

/** The path for reading files. */
export function fsPath(): string | undefined {
const fsPath = (vscode.workspace.workspaceFolders && (vscode.workspace.workspaceFolders.length > 0))
? vscode.workspace.workspaceFolders[0].uri.fsPath : undefined;
if (!fsPath) {
vscode.window.showErrorMessage("Couldn't find the file system path");
return;
}
return fsPath;
}

0 comments on commit 23ef6f1

Please sign in to comment.