Skip to content

Commit 68faa9c

Browse files
committed
build(ui-scripts): make sure bootsrap downloads the design tokens properly and add deep merge to the token build
1 parent 22cc05f commit 68faa9c

5 files changed

Lines changed: 78 additions & 12 deletions

File tree

packages/ui-scripts/lib/build/buildThemes/generateSemantics.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,65 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
import { mergeDeep } from '@instructure/ui-utils'
24+
25+
//////////////////////////////////////////////////////////////////////////////////
26+
// START OF MERGFEDEEP
27+
//////////////////////////////////////////////////////////////////////////////////
28+
//This is needed because scripts can't have dependencies (e.g. ui-utils) because it needs to be resolved before build
29+
30+
function mergeDeep(...args: Record<string, unknown>[]): Record<string, any> {
31+
// note: This could be typed as the union of its args, but since
32+
// its barely used its not worth the effort currently
33+
let target = {}
34+
args.forEach((arg) => {
35+
target = mergeSourceIntoTarget(target, arg)
36+
})
37+
return target
38+
}
39+
40+
function mergeSourceIntoTarget(
41+
target: Record<string, any>,
42+
source: Record<string, any>
43+
) {
44+
if (isObject(source)) {
45+
const keys = [
46+
...Object.keys(source),
47+
...Object.getOwnPropertySymbols(source)
48+
]
49+
const merged = { ...target }
50+
51+
keys.forEach((key: any) => {
52+
if (isObject(target[key]) && isObject(source[key])) {
53+
merged[key] = mergeSourceIntoTarget(target[key], source[key])
54+
} else if (isArray(source[key]) && isArray(target[key])) {
55+
merged[key] = [...new Set([...target[key], ...source[key]])]
56+
} else if (isArray(target[key])) {
57+
merged[key] = [...new Set([...target[key], ...[source[key]]])]
58+
} else {
59+
merged[key] = source[key]
60+
}
61+
})
62+
return merged
63+
} else {
64+
return { ...target }
65+
}
66+
}
67+
68+
function isObject(item: unknown) {
69+
return (
70+
item &&
71+
(typeof item === 'object' || typeof item === 'function') &&
72+
!Array.isArray(item)
73+
)
74+
}
75+
76+
function isArray(item: unknown): boolean {
77+
return Array.isArray(item)
78+
}
79+
80+
//////////////////////////////////////////////////////////////////////////////////
81+
// END OF MERGFEDEEP
82+
//////////////////////////////////////////////////////////////////////////////////
2583

2684
const isReference = (expression: any): boolean =>
2785
expression[0] === '{' && expression[expression.length - 1] === '}'
@@ -106,7 +164,9 @@ export const resolveTypeReferences = (semantics: any, key?: any): string => {
106164
}
107165

108166
export const mergeSemanticSets = (semanticList: any[]) => {
109-
return semanticList.reduce((acc, semantic) => mergeDeep(acc, semantic), {})
167+
return semanticList.reduce((acc, semantic) => {
168+
return mergeDeep(acc, semantic)
169+
}, {})
110170
}
111171

112172
const generateSemantics = (data: any): string => {

packages/ui-scripts/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
"dependencies": {
2323
"@babel/cli": "^7.27.2",
2424
"@instructure/command-utils": "workspace:*",
25-
"@instructure/ui-utils": "workspace:*",
2625
"@instructure/instructure-design-tokens": "github:instructure/instructure-design-tokens",
2726
"@instructure/pkg-utils": "workspace:*",
2827
"@lerna/project": "^6.4.1",

packages/ui-scripts/tsconfig.build.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
},
1616
{
1717
"path": "../pkg-utils/tsconfig.build.json"
18-
},
19-
{ "path": "../ui-utils/tsconfig.build.json" }
18+
}
2019
]
2120
}

pnpm-lock.yaml

Lines changed: 4 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/bootstrap.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ const path = require('path')
2828

2929
const opts = { stdio: 'inherit' }
3030
function buildProject() {
31+
console.info('Fetching design tokens...')
32+
try {
33+
execSync('pnpm update @instructure/instructure-design-tokens -r', opts)
34+
} catch (error) {
35+
console.error(
36+
"'pnpm update @instructure/instructure-design-tokens -r' failed",
37+
error
38+
)
39+
process.exit(1)
40+
}
41+
3142
console.info('Building themes...')
3243
try {
3344
execSync('pnpm run build:themes', opts)

0 commit comments

Comments
 (0)