-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
functionality to add tailwindcss packages to package.json file
- Loading branch information
Sanjiv Kumar
committed
Sep 16, 2020
1 parent
f8e7acf
commit 202e58e
Showing
5 changed files
with
134 additions
and
53 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 |
---|---|---|
@@ -1,62 +1,95 @@ | ||
import { normalize, strings } from '@angular-devkit/core'; | ||
import { workspace } from '@angular-devkit/core/src/experimental'; | ||
import { | ||
apply, | ||
chain, | ||
mergeWith, | ||
move, | ||
Rule, | ||
SchematicContext, | ||
SchematicsException, | ||
template, | ||
Tree, | ||
url, | ||
} from '@angular-devkit/schematics'; | ||
import { chain, Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics'; | ||
import { addPackageJsonDependency, NodeDependency, NodeDependencyType } from '@schematics/angular/utility/dependencies'; | ||
import { Schema } from './schema'; | ||
import { tailwindcssDependencies } from './utils'; | ||
|
||
/** Rule factory: returns a rule (function) */ | ||
export default function (options: Schema): Rule { | ||
// this is a rule (function). It takes a `tree` and returns updated `tree`. | ||
return async (tree: Tree, _context: SchematicContext) => { | ||
return (tree: Tree, context: SchematicContext) => { | ||
console.log('schematic works', options); | ||
// Read `angular.json` as buffer | ||
const workspaceConfigBuffer = tree.read('angular.json'); | ||
if (!workspaceConfigBuffer) { | ||
throw new SchematicsException('Could not find an Angular workspace configuration'); | ||
} | ||
// parse config only when not null | ||
const workspaceConfig: workspace.WorkspaceSchema = JSON.parse(workspaceConfigBuffer.toString()); | ||
// if project is not passed (--project), use default project name | ||
if (!options.project && workspaceConfig.defaultProject) { | ||
options.project = workspaceConfig.defaultProject; | ||
} | ||
const projectName = options.project as string; | ||
// select project from projects array in `angular.json` file | ||
const project: workspace.WorkspaceProject = workspaceConfig.projects[projectName]; | ||
if (!project) { | ||
throw new SchematicsException(`Project ${projectName} is not defined in this workspace.`); | ||
} | ||
const projectType = project.projectType === 'application' ? 'app' : 'lib'; | ||
// // parse config only when not null | ||
// const workspaceConfig: workspace.WorkspaceSchema = JSON.parse(workspaceConfigBuffer.toString()); | ||
// // if project is not passed (--project), use default project name | ||
// if (!options.project && workspaceConfig.defaultProject) { | ||
// options.project = workspaceConfig.defaultProject; | ||
// } | ||
// const projectName = options.project as string; | ||
// const projectName = options.project as string; | ||
// // select project from projects array in `angular.json` file | ||
// const project: workspace.WorkspaceProject = workspaceConfig.projects[projectName]; | ||
// if (!project) { | ||
// throw new SchematicsException(`Project ${projectName} is not defined in this workspace.`); | ||
// } | ||
// const projectType = project.projectType === 'application' ? 'app' : 'lib'; | ||
// Path to create the file | ||
const defaultPath = `${project.sourceRoot}/${projectType}`; | ||
// const defaultPath = `${project.sourceRoot}/${projectType}`; | ||
|
||
// get hold of our templates files | ||
const sourceTemplates = url('./files'); | ||
// const sourceTemplates = url('./files'); | ||
/** | ||
* `template` rule processes templates and returns rule. | ||
* The template helpers like `dasherize` or `classify` are available because we’re spreading `strings` object into the `options` objectand then we’re passing into the template | ||
*/ | ||
const sourceParamteterizedTemplates = apply(sourceTemplates, [ | ||
template({ | ||
...options, | ||
...strings, | ||
name: options.name, | ||
}), | ||
// move file to resolved path | ||
move(normalize(defaultPath as string)), | ||
]); | ||
// const sourceParamteterizedTemplates = apply(sourceTemplates, [ | ||
// template({ | ||
// ...options, | ||
// ...strings, | ||
// name: options.name, | ||
// }), | ||
// // move file to resolved path | ||
// move(normalize(defaultPath as string)), | ||
// ]); | ||
// mergeWith returns a Rule so it can be called with tree and context (not required though) | ||
// merge our template into tree. | ||
return chain([mergeWith(sourceParamteterizedTemplates)]); | ||
return chain([addDependencies(options)])(tree, context); | ||
}; | ||
} | ||
|
||
/** | ||
* Add required dependencies to package.json file. | ||
*/ | ||
function addDependencies(options: Schema): Rule { | ||
const deps = [...tailwindcssDependencies]; | ||
|
||
if (options.cssType !== 'css') { | ||
deps.push(`postcss-${options.cssType}`); | ||
} | ||
return (tree: Tree, context: SchematicContext) => { | ||
deps.map((dependencyName: string) => { | ||
const nodeDependency: NodeDependency = nodeDependencyFactory(dependencyName, options); | ||
addPackageJsonDependency(tree, nodeDependency); | ||
context.logger.info(`✅️ Added ${nodeDependency.name}@${nodeDependency.version} to ${nodeDependency.type}`); | ||
return tree; | ||
}); | ||
}; | ||
} | ||
|
||
// Install dependencies | ||
// function install(): Rule { | ||
// return (tree: Tree, context: SchematicContext) => { | ||
// // Install the dependency | ||
// context.addTask(new NodePackageInstallTask()); | ||
// context.logger.info('✅️ Installed dependencies'); | ||
// return tree; | ||
// }; | ||
// } | ||
|
||
function nodeDependencyFactory(dependencyName: string, options: Schema): NodeDependency { | ||
// default version : latest | ||
let version = 'latest'; | ||
if (dependencyName === 'tailwindcss' && options.tailwindcssVersion) { | ||
version = options.tailwindcssVersion; | ||
} | ||
return { | ||
type: NodeDependencyType.Dev, | ||
name: dependencyName, | ||
version: version, | ||
overwrite: false, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,16 @@ | ||
export interface Schema { | ||
name: string; | ||
/** Name of the project */ | ||
project: string; | ||
/** | ||
* preprocessor for css or type | ||
* @default css | ||
*/ | ||
cssType: cssType; | ||
/** | ||
* tailwindcss version to be installed | ||
* @default latest | ||
*/ | ||
tailwindcssVersion: string; | ||
} | ||
|
||
export type cssType = 'css' | 'scss' | 'sass' | 'less'; |
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,6 @@ | ||
export const tailwindcssDependencies = [ | ||
'tailwindcss', | ||
'postcss-import', | ||
'postcss-loader', | ||
'@angular-builders/custom-webpack', | ||
]; |