This repository has been archived by the owner on Mar 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register assets in angular.json (#10)
* register build and test assets * fix default parser for prettier * configuration interface fixes * logger implementation * register assets only once * code cleanup
- Loading branch information
1 parent
21e3eee
commit 6ed53d2
Showing
7 changed files
with
222 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// partial, contains only the info needed by the tool | ||
|
||
interface AngularConfig { | ||
defaultProject: string; | ||
projects: { | ||
[key: string]: { | ||
architect: { | ||
build: { | ||
options: { | ||
assets: Array<any>; | ||
}; | ||
}; | ||
test: { | ||
options: { | ||
assets: Array<any>; | ||
}; | ||
}; | ||
}; | ||
}; | ||
}; | ||
} | ||
|
||
export default AngularConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
import log from './log'; | ||
import AngularConfig from './angular-config'; | ||
import { Configuration, GlobRule } from './configuration'; | ||
|
||
export function registerAssets(lib: string, config: Configuration) { | ||
if (!config || !config.assets || config.assets.length === 0) { | ||
return; | ||
} | ||
|
||
log.info('registering assets'); | ||
|
||
const configPath = path.join(process.cwd(), 'angular.json'); | ||
let angularConfig: AngularConfig; | ||
|
||
try { | ||
angularConfig = require(configPath); | ||
} catch { | ||
log.warning('angular.json file not found.'); | ||
return; | ||
} | ||
|
||
const project = angularConfig.projects[angularConfig.defaultProject]; | ||
if (!project) { | ||
log.warning(`project ${angularConfig.defaultProject} not found`); | ||
return; | ||
} | ||
|
||
const buildAssets = project.architect.build.options.assets || []; | ||
const testAssets = project.architect.test.options.assets || []; | ||
|
||
for (let rule of config.assets) { | ||
if (typeof rule === 'object') { | ||
registerObject(lib, buildAssets, rule); | ||
registerObject(lib, testAssets, rule); | ||
} | ||
|
||
if (typeof rule === 'string') { | ||
registerString(lib, buildAssets, rule); | ||
registerString(lib, testAssets, rule); | ||
} | ||
} | ||
|
||
project.architect.build.options.assets = buildAssets; | ||
project.architect.test.options.assets = testAssets; | ||
|
||
fs.writeFileSync(configPath, JSON.stringify(angularConfig, null, 2)); | ||
} | ||
|
||
function registerObject( | ||
lib: string, | ||
assets: Array<string | GlobRule>, | ||
rule: GlobRule | ||
) { | ||
if (!isValidObjectRule(rule)) { | ||
log.warning(`skipping invalid object rule`); | ||
return; | ||
} | ||
|
||
const input = path.relative( | ||
process.cwd(), | ||
path.join(process.cwd(), 'node_modules', lib, rule.input) | ||
); | ||
|
||
const finalRule = { ...rule, input }; | ||
|
||
if (!isRegisteredObject(assets, finalRule)) { | ||
assets.push(finalRule); | ||
} | ||
} | ||
|
||
function registerString( | ||
lib: string, | ||
assets: Array<string | GlobRule>, | ||
rule: string | ||
) { | ||
if (rule && assets) { | ||
const input = path.relative( | ||
process.cwd(), | ||
path.join(process.cwd(), 'node_modules', lib, rule) | ||
); | ||
|
||
if (!isRegisteredString(assets, input)) { | ||
assets.push(input); | ||
} | ||
} | ||
} | ||
|
||
function isValidObjectRule(rule: GlobRule): boolean { | ||
if (rule && rule.glob && rule.input && rule.output) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
function isRegisteredString( | ||
assets: Array<string | GlobRule>, | ||
rule: string | ||
): boolean { | ||
const stringRules = assets.filter(entry => typeof entry === 'string'); | ||
return stringRules.includes(rule); | ||
} | ||
|
||
function isRegisteredObject( | ||
assets: Array<string | GlobRule>, | ||
rule: GlobRule | ||
): boolean { | ||
const objectRules = assets | ||
.filter(entry => typeof entry === 'object') | ||
.map(entry => entry as GlobRule); | ||
|
||
return objectRules.some(existing => { | ||
return ( | ||
existing.glob === rule.glob && | ||
existing.input === rule.input && | ||
existing.output === rule.output | ||
); | ||
}); | ||
} | ||
|
||
/* | ||
export function copyAssets(lib: string, config: Configuration): void { | ||
if (config && config.assets && config.assets.length > 0) { | ||
const libPath = path.join(process.cwd(), 'node_modules', lib); | ||
const localPath = path.join(process.cwd(), 'src/assets'); | ||
sh.mkdir('-p', localPath); | ||
config.assets.forEach(asset => { | ||
const from = path.join(libPath, asset.from); | ||
const to = path.join(localPath, asset.to || ''); | ||
sh.mkdir('-p', to); | ||
console.log(chalk.blue('info'), `copy ${asset.from}`); | ||
sh.cp(from, to); | ||
}); | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
interface Configuration { | ||
assets?: Array<{ | ||
from: string; | ||
to?: string; | ||
}>; | ||
export interface Configuration { | ||
assets?: Array<string | GlobRule>; | ||
|
||
modules?: Array<{ | ||
name: string; | ||
namespace: string; | ||
}>; | ||
} | ||
|
||
export default Configuration; | ||
export interface GlobRule { | ||
glob: string; | ||
input: string; | ||
output: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import chalk from 'chalk'; | ||
|
||
class Log { | ||
success(message: string): void { | ||
console.log(chalk.green('success'), message); | ||
} | ||
warning(message: string): void { | ||
console.log(chalk.yellow('warning'), message); | ||
} | ||
|
||
info(message: string): void { | ||
console.log(chalk.blue('info'), message); | ||
} | ||
|
||
error(message: string): void { | ||
console.log(chalk.red('error'), message); | ||
} | ||
} | ||
|
||
export default new Log(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters