Skip to content

Commit

Permalink
Support Windows line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
kmturley committed Oct 14, 2023
1 parent b0b0da2 commit 53c0c97
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/convert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { js2xml, xml2js } from 'xml-js';
import { parseSfz } from './parse';
import { ParseDefinition, ParseHeader, ParseOpcode } from './types/parse';
import { LINE_END, normalizeXml } from './utils';

const declaration: any = {
attributes: {
Expand All @@ -19,17 +20,17 @@ const OPTIONS_XML: any = {
function convertJsToSfz(jsObj: ParseDefinition) {
let sfzText: string = '';
jsObj.elements.forEach((header: ParseHeader) => {
sfzText += `<${header.name}>\n`;
sfzText += `<${header.name}>${LINE_END}`;
header.elements.forEach((opcode: ParseOpcode) => {
sfzText += `${opcode.attributes.name}=${opcode.attributes.value}\n`;
sfzText += `${opcode.attributes.name}=${opcode.attributes.value}${LINE_END}`;
});
});
return sfzText;
}

function convertJsToXml(jsObj: ParseDefinition) {
const xml: string = js2xml(jsObj, OPTIONS_XML);
return xml.replace(/\/>/g, ' />') + '\n';
return normalizeXml(xml);
}

async function convertSfzToJs(sfzFile: string, prefix = '') {
Expand All @@ -49,7 +50,7 @@ async function convertSfzToXml(sfzFile: string, prefix = '') {
},
OPTIONS_XML
);
return xml.replace(/\/>/g, ' />') + '\n';
return normalizeXml(xml);
}

function convertXmlToJs(xmlFile: string) {
Expand Down
15 changes: 12 additions & 3 deletions src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ function dirContains(dirParent: string, dirChild: string): boolean {
return relative && !relative.startsWith('..') && !path.isAbsolute(relative) ? true : false;
}

function dirCreate(dirPath: string): string | undefined {
log('+', dirPath);
return mkdirSync(dirPath, { recursive: true });
function dirCreate(dirPath: string): string | boolean {
if (!dirExists(dirPath)) {
log('+', dirPath);
mkdirSync(dirPath, { recursive: true });
return dirPath;
}
return false;
}

function dirDelete(dirPath: string): void {
Expand Down Expand Up @@ -70,6 +74,11 @@ function dirOpen(dirPath: string): Buffer {

function dirRead(dirPath: string, options?: any): string[] {
log('⌕', dirPath);
// Glob now expects forward slashes on Windows
// Convert backslashes from path.join() to forwardslashes
if (process.platform === 'win32') {
dirPath = dirPath.replace(/\\/g, '/');
}
return globSync(dirPath, options);
}

Expand Down
20 changes: 19 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const IS_WIN: boolean = typeof process !== 'undefined' && process.platform === 'win32';
const LINE_END: string = IS_WIN ? '\r\n' : '\n';
let LOGGING_ENABLED: boolean = false;

function log(...args: any) {
Expand All @@ -14,6 +16,11 @@ function logDisable(...args: any) {
LOGGING_ENABLED = false;
}

function normalizeXml(input: string) {
input = input.replace(/\n/g, LINE_END);
return input.replace(/\/>/g, ' />') + LINE_END;
}

function pathGetDirectory(pathItem: string, separator: string = '/'): string {
return pathItem.substring(0, pathItem.lastIndexOf(separator));
}
Expand Down Expand Up @@ -61,4 +68,15 @@ function pathJoin(...segments: any) {
return resultParts.join('/');
}

export { log, logEnable, logDisable, pathGetDirectory, pathGetExt, pathGetFilename, pathJoin };
export {
IS_WIN,
LINE_END,
log,
logEnable,
logDisable,
normalizeXml,
pathGetDirectory,
pathGetExt,
pathGetFilename,
pathJoin,
};
6 changes: 3 additions & 3 deletions tests/convert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
convertSfzToXml,
convertXmlToJs,
convertXmlToSfz,
} from '../dist/convert';
import { fileReadJson, fileReadString } from '../dist/file';
import { ParseDefinition } from '../dist/types/parse';
} from '../src/convert';
import { fileReadJson, fileReadString } from '../src/file';
import { ParseDefinition } from '../src/types/parse';

const syntaxDir: string = path.join('test', 'syntax');
const sfzJs: ParseDefinition = fileReadJson(path.join(syntaxDir, 'basic.json'));
Expand Down
2 changes: 1 addition & 1 deletion tests/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('Create new directory', () => {
});

test('Create existing directory', () => {
expect(dirCreate(DIR_PATH)).toEqual(undefined);
expect(dirCreate(DIR_PATH)).toEqual(false);
});

test('Directory is empty', () => {
Expand Down
4 changes: 2 additions & 2 deletions tests/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { apiText } from '../src/api';
import { js2xml } from 'xml-js';
import { dirRead, fileReadString } from '../src/file';
import path from 'path';
import { normalizeXml } from '../src/utils';

function convertToXml(elements: any) {
const xml: string = js2xml(
Expand All @@ -26,8 +27,7 @@ function convertToXml(elements: any) {
},
{ spaces: '\t' }
);
// TODO do better
return xml.replace(/\/>/g, ' />') + '\n';
return normalizeXml(xml);
}

// Test specific syntax edge-cases
Expand Down

0 comments on commit 53c0c97

Please sign in to comment.