Skip to content

Commit

Permalink
feature: Adding ability to sort the import map alphabetically (#122)
Browse files Browse the repository at this point in the history
* feature: Adding ability to sort the import map alphabetically

* Adding readme documentation

* PR feedback - Fixing test name

Co-authored-by: the_mcmurder <the_mcmurder@users.noreply.github.com>
  • Loading branch information
TheMcMurder and the_mcmurder committed Aug 17, 2021
1 parent dc040ec commit 3f3a990
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ Here are the properties available in the config file:
own way of writing the import map. The function must return a Promise that resolves with the import map as an object. Since javascript functions are
not part of JSON, this option is only available if you provide a config.js file (instead of config.json).
- `cacheControl` (optional): Cache-control header that will be set on the import map file when the import-map-deployer is called. Defaults to `public, must-revalidate, max-age=0`.
- `alphabetical` (optional, defaults to false): A boolean that indicates whether to sort the import-map alphabetically by service/key/name.

### Option 1: json file

Expand Down
32 changes: 29 additions & 3 deletions src/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ function modifyLock(env, modifierFunc) {
}

exports.modifyImportMap = function (env, newValues) {
const { services: newImports, scopes: newScopes } = newValues;
const { services, scopes } = newValues;

const alphabetical = !!getConfig().alphabetical;
const newImports =
services && typeof services === "object" && alphabetical
? sortObjectAlphabeticallyByKeys(services)
: services;
const newScopes =
scopes && typeof scopes === "object" && alphabetical
? sortObjectAlphabeticallyByKeys(scopes)
: scopes;

// either imports or scopes have to be defined
if (newImports || newScopes) {
Expand Down Expand Up @@ -135,9 +145,25 @@ exports.modifyService = function (
map[serviceName + "/"] = address;
}
}

return json;
const alphabetical = !!getConfig().alphabetical;
if (alphabetical) {
return {
imports: sortObjectAlphabeticallyByKeys(json.imports),
scopes: sortObjectAlphabeticallyByKeys(json.scopes),
};
} else {
return json;
}
});
};

exports.getEmptyManifest = getEmptyManifest;

function sortObjectAlphabeticallyByKeys(unordered) {
return Object.keys(unordered)
.sort()
.reduce((obj, key) => {
obj[key] = unordered[key];
return obj;
}, {});
}
116 changes: 116 additions & 0 deletions test/alphabetical-import-map.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
const request = require("supertest");
const { app, setConfig } = require("../src/web-server");
const {
resetManifest: resetMemoryManifest,
} = require("../src/io-methods/memory");

describe(`alphabetically sorted`, () => {
beforeAll(() => {
setConfig({
manifestFormat: "importmap",
alphabetical: true,
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
});
});

beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
const setupRequest = request(app)
.patch("/import-map.json")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
imports: {
c: "/c-1.mjs",
b: "/b-1.mjs",
},
})
.expect(200)
.expect("Content-Type", /json/);
return setupRequest.then((response) => {
expect(JSON.stringify(response.body)).toBe(
`{"imports":{"b":"/b-1.mjs","c":"/c-1.mjs"},"scopes":{}}`
);
});
});

it(`should place the import into the map alphabetically instead of just at the end`, async () => {
const response = await request(app)
.patch("/services")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
service: "a",
url: "/a-1-updated.mjs",
})
.expect(200)
.expect("Content-Type", /json/);

expect(JSON.stringify(response.body.imports)).toBe(
`{"a":"/a-1-updated.mjs","b":"/b-1.mjs","c":"/c-1.mjs"}`
);
});
});

describe(`not alphabetically sorted`, () => {
beforeAll(() => {
setConfig({
manifestFormat: "importmap",
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
});
});

beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
const setupRequest = request(app)
.patch("/import-map.json")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
imports: {
c: "/c-1.mjs",
b: "/b-1.mjs",
},
})
.expect(200)
.expect("Content-Type", /json/);
return setupRequest.then((response) => {
expect(JSON.stringify(response.body)).toBe(
`{"imports":{"c":"/c-1.mjs","b":"/b-1.mjs"},"scopes":{}}`
);
});
});

it(`should not place things alphabetically and should just append to the end`, async () => {
const response = await request(app)
.patch("/services")
.query({
skip_url_check: true,
})
.set("accept", "json")
.send({
service: "a",
url: "/a-1-updated.mjs",
})
.expect(200)
.expect("Content-Type", /json/);

expect(JSON.stringify(response.body.imports)).toBe(
`{"c":"/c-1.mjs","b":"/b-1.mjs","a":"/a-1-updated.mjs"}`
);
});
});
30 changes: 16 additions & 14 deletions test/import-map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ const {
resetManifest: resetMemoryManifest,
} = require("../src/io-methods/memory");

beforeAll(() => {
setConfig({
manifestFormat: "importmap",
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
describe(`/import-map.json`, () => {
let errorSpy;
beforeAll(() => {
setConfig({
manifestFormat: "importmap",
packagesViaTrailingSlashes: true,
locations: {
prod: "memory://prod",
},
});
});
beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
errorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
errorSpy.mockClear();
});
});

beforeEach(() => {
// assure we have a clean import map every test
resetMemoryManifest();
});

describe(`/import-map.json`, () => {
it(`does not return anything when it's not setup yet.`, async () => {
const response = await request(app)
.get("/import-map.json")
Expand Down

0 comments on commit 3f3a990

Please sign in to comment.