Skip to content

Commit

Permalink
Proper TS Migration - CommonJS
Browse files Browse the repository at this point in the history
Going to go back to the ground level again to start this TS conversion.

I recently learned with some other TS conversions that I was doing before this one (this was pretty much my first), I think I was doing some of the things in the wrong order.

It's absolutely crucial to preserve the state of your tests while they are still working, when moving code over. This is only going to be of help for when you change things later.

I should have waited longer before moving to pure ESM, and should have done the TS part first, sticking with CommonJS. Once things are being tested accordingly in CJS, I think it's not too far to ensure that moving to ESM isn't something too far off. And coming back to this project, I just realized how much is already done actually! Everything was written in Flow, which is very close to TS, so not much work has to be done by me on that front.

Listening to the bonus tracks from Empath, I'm really getting into it this time! It hasn't initially clicked with me too much the first few times. Now I think I'm liking them more.

I'm also going to try making less opinionated decisions during the migration, on my own behalf. I shouldn't be formatting things, that's not the goal here. I can be doing that either at a much later date, or just not at all. It's also not going to help with discerning changes I made in commits over time, and that's the exact issue I had in coming back to this one. I'm having a hard time figuring out when the tests stopped working, and letting that happen has gotten us to where we are now. They worked before, so I just have to ensure they keep working.
  • Loading branch information
Offroaders123 committed Feb 22, 2024
1 parent 04473dc commit 628c0cb
Show file tree
Hide file tree
Showing 23 changed files with 98 additions and 111 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"email": "jesse.ditson@gmail.com"
}
],
"type": "commonjs",
"main": "build2/jsmediatags.js",
"browser": "dist/jsmediatags.js",
"repository": {
Expand Down
4 changes: 2 additions & 2 deletions src/ArrayBufferFileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*/
'use strict';

const ChunkedFileData = require('./ChunkedFileData');
const MediaFileReader = require('./MediaFileReader');
import ChunkedFileData = require('./ChunkedFileData');
import MediaFileReader = require('./MediaFileReader');

import type {
LoadCallbackType
Expand Down
6 changes: 2 additions & 4 deletions src/ArrayFileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
*/
'use strict';

var MediaFileReader = require('./MediaFileReader');
import MediaFileReader = require('./MediaFileReader');

import type {
Byte,
ByteArray,
LoadCallbackType
} from './FlowTypes';

class ArrayFileReader extends MediaFileReader {
export = class ArrayFileReader extends MediaFileReader {
_array: ByteArray;
_size: number;

Expand Down Expand Up @@ -44,5 +44,3 @@ class ArrayFileReader extends MediaFileReader {
return this._array[offset];
}
}

module.exports = ArrayFileReader;
8 changes: 3 additions & 5 deletions src/BlobFileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*/
'use strict';

const ChunkedFileData = require('./ChunkedFileData');
const MediaFileReader = require('./MediaFileReader');
import ChunkedFileData = require('./ChunkedFileData');
import MediaFileReader = require('./MediaFileReader');

import type {
LoadCallbackType
} from './FlowTypes';

class BlobFileReader extends MediaFileReader {
export = class BlobFileReader extends MediaFileReader {
_blob: Blob;
_fileData: ChunkedFileData;

Expand Down Expand Up @@ -60,5 +60,3 @@ class BlobFileReader extends MediaFileReader {
return this._fileData.getByteAt(offset);
}
}

module.exports = BlobFileReader;
12 changes: 6 additions & 6 deletions src/ByteArrayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ const getInteger24 = function(number: number): ByteArray {
];
}

module.exports = {
bin: bin,
pad: pad,
getSynchsafeInteger32: getSynchsafeInteger32,
getInteger32: getInteger32,
getInteger24: getInteger24
export {
bin,
pad,
getSynchsafeInteger32,
getInteger32,
getInteger24
};
4 changes: 1 addition & 3 deletions src/ChunkedFileData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type {
DataType
} from './FlowTypes';

class ChunkedFileData {
export = class ChunkedFileData {
// $FlowIssue - get/set properties not yet supported
static get NOT_FOUND() { return NOT_FOUND; }
_fileData: Array<ChunkType>;
Expand Down Expand Up @@ -213,5 +213,3 @@ class ChunkedFileData {
throw new Error("Offset " + offset + " hasn't been loaded yet.");
}
}

module.exports = ChunkedFileData;
12 changes: 5 additions & 7 deletions src/FLACTagContents.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const ByteArrayUtils = require('./ByteArrayUtils');
import ByteArrayUtils = require('./ByteArrayUtils');

const bin = require('./ByteArrayUtils').bin;
const getInteger24 = require('./ByteArrayUtils').getInteger24;
const getInteger32 = require('./ByteArrayUtils').getInteger32;
import { bin } from './ByteArrayUtils';
import { getInteger24 } from './ByteArrayUtils';
import { getInteger32 } from './ByteArrayUtils';

import type {
ByteArray
} from './FlowTypes';

class FLACTagContents {
export = class FLACTagContents {
_blocks: Array<MetadataBlock>;

constructor(blocks?: Array<MetadataBlock>) {
Expand Down Expand Up @@ -74,5 +74,3 @@ class MetadataBlock {
return [ this._type + (this._final ? 128 : 0) ].concat(this._data);
}
}

module.exports = FLACTagContents;
6 changes: 2 additions & 4 deletions src/FLACTagReader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var MediaTagReader = require('./MediaTagReader');
import MediaTagReader = require('./MediaTagReader');

/* The first 4 bytes of a FLAC file describes the header for the file. If these
* bytes respectively read "fLaC", we can determine it is a FLAC file.
Expand Down Expand Up @@ -60,7 +60,7 @@ import type {
/**
* Class representing a MediaTagReader that parses FLAC tags.
*/
class FLACTagReader extends MediaTagReader {
export = class FLACTagReader extends MediaTagReader {
_commentOffset: number;
_pictureOffset: number;

Expand Down Expand Up @@ -338,5 +338,3 @@ class FLACTagReader extends MediaTagReader {
return tag;
}
}

module.exports = FLACTagReader;
2 changes: 1 addition & 1 deletion src/FlowTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @flow
*/

var MediaFileReader = require('./MediaFileReader');
import MediaFileReader = require('./MediaFileReader');

export type CallbackType = {
onSuccess: (data: any) => void,
Expand Down
8 changes: 3 additions & 5 deletions src/ID3v1TagReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
*/
'use strict';

var MediaTagReader = require('./MediaTagReader');
var MediaFileReader = require('./MediaFileReader');
import MediaTagReader = require('./MediaTagReader');
import MediaFileReader = require('./MediaFileReader');

import type {
LoadCallbackType,
ByteRange,
TagType
} from './FlowTypes';

class ID3v1TagReader extends MediaTagReader {
export = class ID3v1TagReader extends MediaTagReader {
static getTagIdentifierByteRange(): ByteRange {
// The identifier is TAG and is at offset: -128. However, to avoid a
// fetch for the tag identifier and another for the data, we load the
Expand Down Expand Up @@ -105,5 +105,3 @@ var GENRES = [
"Folklore","Ballad","Power Ballad","Rhythmic Soul","Freestyle",
"Duet","Punk Rock","Drum Solo","Acapella","Euro-House","Dance Hall"
];

module.exports = ID3v1TagReader;
10 changes: 4 additions & 6 deletions src/ID3v2FrameReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
'use strict';

var MediaFileReader = require('./MediaFileReader');
const StringUtils = require('./StringUtils');
var ArrayFileReader = require('./ArrayFileReader');
import MediaFileReader = require('./MediaFileReader');
import { StringUtils } from './StringUtils';
import ArrayFileReader = require('./ArrayFileReader');

import type {
CharsetType,
Expand Down Expand Up @@ -175,7 +175,7 @@ const FRAME_DESCRIPTIONS = {
"WXXX" : "User defined URL link frame"
};

class ID3v2FrameReader {
export = class ID3v2FrameReader {
static getFrameReaderFunction(frameId: string): ?FrameReaderSignature {
if (frameId in frameReaderFunctions) {
return frameReaderFunctions[frameId];
Expand Down Expand Up @@ -707,5 +707,3 @@ var PICTURE_TYPE = [
"Band/artist logotype",
"Publisher/Studio logotype"
];

module.exports = ID3v2FrameReader;
15 changes: 7 additions & 8 deletions src/ID3v2TagContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
*/
'use strict';

const ByteArrayUtils = require('./ByteArrayUtils');
const bin = ByteArrayUtils.bin;
const getSynchsafeInteger32 = ByteArrayUtils.getSynchsafeInteger32;
const getInteger32 = ByteArrayUtils.getInteger32;
const getInteger24 = ByteArrayUtils.getInteger24;
import {
bin,
getSynchsafeInteger32,
getInteger32,
getInteger24
} from './ByteArrayUtils';

// Offsets
const FLAGS = 5;
Expand All @@ -34,7 +35,7 @@ import type {
TagFrameFlags
} from './FlowTypes';

class ID3v2TagContents {
export = class ID3v2TagContents {
_size: number;
_major: number;
_revision: number;
Expand Down Expand Up @@ -398,5 +399,3 @@ class ID3v2TagContents {
].concat(data));
}
}

module.exports = ID3v2TagContents;
10 changes: 4 additions & 6 deletions src/ID3v2TagReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*/
'use strict';

var MediaTagReader = require('./MediaTagReader');
var MediaFileReader = require('./MediaFileReader');
var ID3v2FrameReader = require('./ID3v2FrameReader');
import MediaTagReader = require('./MediaTagReader');
import MediaFileReader = require('./MediaFileReader');
import ID3v2FrameReader = require('./ID3v2FrameReader');

import type {
CallbackType,
Expand All @@ -21,7 +21,7 @@ import type {

const ID3_HEADER_SIZE = 10;

class ID3v2TagReader extends MediaTagReader {
export = class ID3v2TagReader extends MediaTagReader {
static getTagIdentifierByteRange(): ByteRange {
// ID3 header
return {
Expand Down Expand Up @@ -149,5 +149,3 @@ const SHORTCUTS = {
"picture" : ["APIC", "PIC"],
"lyrics" : ["USLT", "ULT"]
};

module.exports = ID3v2TagReader;
13 changes: 6 additions & 7 deletions src/MP4TagContents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
*/
'use strict';

const ByteArrayUtils = require('./ByteArrayUtils');
const bin = ByteArrayUtils.bin;
const pad = ByteArrayUtils.pad;
const getInteger32 = ByteArrayUtils.getInteger32;
import {
bin,
pad,
getInteger32
} from './ByteArrayUtils';

import type {
ByteArray
} from './FlowTypes';

class MP4TagContents {
export = class MP4TagContents {
_atoms: Array<Atom>;

constructor(ftyp: string, atoms?: Array<Atom>) {
Expand Down Expand Up @@ -84,5 +85,3 @@ class Atom {
);
}
}

module.exports = MP4TagContents;
8 changes: 3 additions & 5 deletions src/MP4TagReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/
'use strict';

var MediaTagReader = require('./MediaTagReader');
var MediaFileReader = require('./MediaFileReader');
import MediaTagReader = require('./MediaTagReader');
import MediaFileReader = require('./MediaFileReader');

import type {
CallbackType,
Expand All @@ -20,7 +20,7 @@ import type {
TagFrame
} from './FlowTypes';

class MP4TagReader extends MediaTagReader {
export = class MP4TagReader extends MediaTagReader {
static getTagIdentifierByteRange(): ByteRange {
// The tag identifier is located in [4, 8] but since we'll need to reader
// the header of the first block anyway, we load it instead to avoid
Expand Down Expand Up @@ -351,5 +351,3 @@ const SHORTCUTS = {
"picture" : "covr",
"lyrics" : "©lyr"
};

module.exports = MP4TagReader;
6 changes: 2 additions & 4 deletions src/MediaFileReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
'use strict';

const StringUtils = require('./StringUtils');
import { StringUtils } from './StringUtils';

import type {
DecodedString
Expand All @@ -14,7 +14,7 @@ import type {
CharsetType
} from './FlowTypes';

class MediaFileReader {
export = class MediaFileReader {
_isInitialized: boolean;
_size: number;

Expand Down Expand Up @@ -220,5 +220,3 @@ class MediaFileReader {
return size;
}
}

module.exports = MediaFileReader;
6 changes: 2 additions & 4 deletions src/MediaTagReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
'use strict';

const MediaFileReader = require('./MediaFileReader');
import MediaFileReader = require('./MediaFileReader');

import type {
CallbackType,
Expand All @@ -12,7 +12,7 @@ import type {
TagType
} from './FlowTypes';

class MediaTagReader {
export = class MediaTagReader {
_mediaFileReader: MediaFileReader;
_tags: ?Array<string>;

Expand Down Expand Up @@ -108,5 +108,3 @@ class MediaTagReader {
return tags;
}
}

module.exports = MediaTagReader;
Loading

0 comments on commit 628c0cb

Please sign in to comment.