Skip to content

Commit 5aa9bb5

Browse files
committed
Refactor: make save and generateMapping promise first
1 parent 321e95f commit 5aa9bb5

File tree

5 files changed

+18
-57
lines changed

5 files changed

+18
-57
lines changed

__tests__/generateMapping.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ test('should overwrite mapping files', (done) => {
9191
});
9292

9393
test('should not overwrite mapping files', async () => {
94-
await expect(generateMapping(testCwd.name)).resolves.toBeTruthy();
94+
await expect(generateMapping(testCwd.name)).resolves.toBe(undefined);
9595
await expect(generateMapping(testCwd.name)).rejects.toBeTruthy();
9696
});
9797

__tests__/processAuto.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ test('should not overwrite original files', async () => {
8282
newPath: fixturesCwd,
8383
cwd: fixturesCwd,
8484
})
85-
)).rejects.toEqual({
86-
message: 'File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.',
87-
});
85+
)).rejects.toEqual(new Error('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.'));
8886
});
8987

9088
test('should fail', async () => {

__tests__/save.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,15 @@ test('should create a file within a non existing dir', (done) => {
2929
});
3030
});
3131

32-
test('should not overwrite the same file', async (done) => {
32+
test('should not overwrite the same file', async () => {
3333
const filePath = path.join(testFiles, '/config.json');
3434
const filePathTest = path.join(testCwd.name, '/config.json');
3535
const oldFile = fs.readFileSync(filePath, 'utf8');
3636

3737
await save(filePathTest, 'test content');
3838

39-
save(filePathTest, 'test content', (err) => {
40-
expect(err.message).toBe('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.');
41-
expect(fs.readFileSync(filePath, 'utf8')).toBe(oldFile);
42-
43-
done();
44-
});
39+
await expect(save(filePathTest, 'test content'))
40+
.rejects
41+
.toEqual(new Error('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.'));
42+
expect(fs.readFileSync(filePath, 'utf8')).toBe(oldFile);
4543
});

lib/helper/save.ts

+8-29
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,23 @@
1-
import { fromCallback } from 'universalify';
1+
import { fromPromise } from 'universalify';
22
import fs from 'fs-extra';
33
import path from 'path';
44

55
interface SaveOptions {
66
overwrite?: boolean;
77
}
88

9-
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
10-
const save = (
9+
const save = async (
1110
destinationPath: string,
1211
data: string,
13-
opts: SaveOptions | Callback,
14-
cb?: Callback,
15-
) => {
12+
options: SaveOptions = {},
13+
): Promise<void> => {
1614
// @todo check if the filepath has an .ext
17-
let callback = cb as Callback;
18-
let options = opts;
19-
20-
// set cb if options are not set
21-
if (typeof options === 'function') {
22-
callback = options;
23-
options = {};
24-
}
25-
2615
if (!options.overwrite && fs.existsSync(destinationPath)) {
27-
// todo jpeer: throw error
28-
return callback({
29-
message: 'File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.',
30-
});
16+
throw new Error('File exist and cannot be overwritten. Set the option overwrite to true to overwrite files.');
3117
}
3218

33-
return fs.mkdirs(path.dirname(destinationPath), () => {
34-
fs.writeFile(destinationPath, data, (err) => {
35-
if (err) {
36-
return callback(err);
37-
}
38-
39-
return callback(null, `Successfully wrote ${destinationPath}`);
40-
});
41-
});
19+
await fs.mkdirs(path.dirname(destinationPath));
20+
await fs.writeFile(destinationPath, data);
4221
}; // /save
4322

44-
export default fromCallback(save);
23+
export default fromPromise(save);

lib/mapping/generateMapping.ts

+3-17
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ export interface GenerateMappingOptions {
1414
overwrite?: boolean;
1515
}
1616

17-
const generateMapping = (
17+
const generateMapping = async (
1818
pathString: string,
1919
opts: GenerateMappingOptions,
20-
cb: Callback,
21-
): void => {
20+
): Promise<void> => {
2221
let fileName = 'renaming_map';
2322
let fileNameExt = '.json';
2423
let mappingName = 'CSS_NAME_MAPPING';
@@ -33,13 +32,6 @@ const generateMapping = (
3332
...opts,
3433
};
3534

36-
let callback = cb;
37-
38-
// set cb if options are not set
39-
if (typeof callback !== 'function') {
40-
callback = options as () => void;
41-
}
42-
4335
if (options.cssMappingMin) {
4436
options.origValues = false;
4537
mappingName = 'CSS_NAME_MAPPING_MIN';
@@ -70,13 +62,7 @@ const generateMapping = (
7062
fileNameExt = '.js';
7163
}
7264

73-
save(`${newPath}${fileNameExt}`, writeData, { overwrite: options.overwrite }, (err: any, data: string) => {
74-
if (err) {
75-
callback(err);
76-
}
77-
78-
callback(null, data);
79-
});
65+
await save(`${newPath}${fileNameExt}`, writeData, { overwrite: options.overwrite });
8066
}; // /generateMapping
8167

8268
export default fromPromise(generateMapping);

0 commit comments

Comments
 (0)